1

I have a "object reference not set to an instance of an object unity" errors even though I have an instance of object in project.

26: teslaGun = GameObject.Find("TeslaGun");
27: teslaGunController = teslaGun.GetComponent<TeslaGunController>(); 

35: arms = GameObject.Find("Arms").gameObject;

47: if (arms.activeSelf)

Here are errors Here are objects

3 Answers3

0

Try this

teslaGun = GameObject.Find("Player/TeslaGun");
arms = GameObject.Find("Player/Arms");
vasmos
  • 2,472
  • 1
  • 10
  • 21
0

It isn't a good idea to find a Gameobject with its name. You can use FindObjectOfType instead of Find. Also you can make teslaGam and arms public and set them in inspector.

Abolfazl
  • 100
  • 1
  • 9
0

Well, I solved it by having (I suppose) some monkey coding.

void Start()
    {
        player = GameObject.Find("Player");
        playerController = player.GetComponent<PlayerController>();

        teslaGun = player.transform.Find("TeslaGun").gameObject;
        //teslaGun = GameObject.Find("Player/TeslaGun");
        teslaGunController = teslaGun.GetComponent<TeslaGunController>();

        shotGun = player.transform.Find("ShotGun").gameObject;
        //shotGun = GameObject.Find("Player/ShotGun");
        shotGunController = shotGun.GetComponent<ShotGunController>();

        subMachineGun = player.transform.Find("SubMachineGun").gameObject;
        //subMachineGun = GameObject.Find("Player/SubMachineGun");
        sMGController = subMachineGun.GetComponent<SMGController>();

        arms = player.transform.Find("Arms").gameObject;
        //arms = GameObject.Find("Player/Arms").gameObject;

    }