0

Yo estaba viendo un tutorial de como agregarle una pistola a un juego de unity, hice (según yo) todos los pasos bien pero cuando agrego el script al jugador y intento poner el modelo de la pistola no me deja, aquí esta el código y una imagen de lo que me pasa: )

En el lado izquierdo de la imagen esta el modelo y a la derecha esta el script, lo que no funciona es el element 0 (no me deja arrastrar el model al element 0)

Aquí el código:

using System.Collections.Generic;
using UnityEngine;

public class PlayerWeaponController : MonoBehaviour
{
    public List<WeaponController> startingWeapons = new List<WeaponController>();

    public Transform weaponParentSocket;
    public Transform defaultWeaponPosition;
    public Transform aimingPosition;

    public int activeWeaponIndex { get; private set; }
    
    private WeaponController[] weaponSlots = new WeaponController[5];
    

    // Start is called before the first frame update
    void Start()
    {
        activeWeaponIndex = -1;

        foreach (WeaponController startingWeapon in startingWeapons)
        {
            AddWeapon(startingWeapon);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Alpha1))
        {
            SwitchWeapon(0);
        }




    }

    private void SwitchWeapon(int p_weaponIndex)
    {
        if (p_weaponIndex != activeWeaponIndex && p_weaponIndex >= 0)
        {
            weaponSlots[p_weaponIndex].gameObject.SetActive(true);
            activeWeaponIndex = p_weaponIndex;
        }


    }

    private void AddWeapon(WeaponController p_weaponPrefab)
    {
        weaponParentSocket.position = defaultWeaponPosition.position;

        for (int i = 0; i<weaponSlots.Length; i++)
        {
            if (weaponSlots[i] == null)
            {
                WeaponController weaponClone = Instantiate(p_weaponPrefab, weaponParentSocket);
                weaponClone.gameObject.SetActive(false);


                weaponSlots[i] = weaponClone;
                return;
            }
        }

    }
}

1 respuesta 1

0

Lo que esta ocurriendo es que le estas pasando como argumento un objeto prefabricado (GameObject) mientras que en el inspector estaría requiriendo un tipo WeaponController al cual no puede acceder si el objeto no se encuentra en la escena, si agregas el objeto prefabricado a la escena y este posee un MonoBehaviour de tipo WeaponController deberá acceder al mismo de manera correcta, sin embargo si pretendes un instanciamiento de estos deberás cambiar el tipo de tu lista de

List<WeaponController> startingWeapons -> List<GameObject> startingWeapons

Y luego acceder a sus propiedades desde el script. Saludos, suerte en tu aprendizaje.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.