#gameobject.Getcomponent<Animator>(); yielding no animator...?

1 messages · Page 1 of 1 (latest)

lost pivot
#

Having a little issue with:

        weapon = GameObject.FindGameObjectWithTag("Weapon");
        weaponAnim = weapon.GetComponent<Animator>();
    }```

https://prnt.sc/cIm-39AEw8Ju

https://prnt.sc/utxcYlC-ddWD
Lightshot

Captured with Lightshot

Lightshot

Captured with Lightshot

#

<ignore the yellowed out field>

#

gameobject.Getcomponent<Animator>(); yielding no animator...?

#

UNSOLVED + temporary ANSWER:

Was calling for the animator in the same instant that i was calling for the game object, which means the game object was not yet called and so "weapon" was an unassigned game object!

temporary solution:
weaponAnim = weapon.GetComponent<Animator>();
in update event;

lost pivot
#

        ///weapon = GameObject.FindGameObjectWithTag("Weapon");

        ///weaponAnim = weapon.GetComponent<Animator>();

        StartCoroutine(WeaponGetter());
        
    }




IEnumerator WeaponGetter()
{

    Debug.Log("started coroutine weaponGetter");
         weapon = GameObject.FindGameObjectWithTag("Weapon"); //I've tagged the game object Weapon with the tag "Weapon"

         

//returning 0 will make it wait 1 frame
 yield return 0;
         weaponAnim = weapon.GetComponent<Animator>();
 
 
}```
north gale
#

Well for starters the method should be named Start not start

lost pivot
#

facepalm

north gale
#

And the weapon should be tagged appropriately

lost pivot
#

well that worked.

vivid saffron
#

why would you wait one frame?

lost pivot
#

Thank you lol

vivid saffron
#

doesnt make much sense for me

north gale
#

Np

lost pivot
#

Waiting one frame because if i dont, the game object doesnt exist for weaponAnim = --> weapon <--- .GetComponent<Animator>();

vivid saffron
#

I dont think you need it

#

void WeaponGetter()
{

Debug.Log("started coroutine weaponGetter");
     weapon = GameObject.FindGameObjectWithTag("Weapon"); //I've tagged the game object Weapon with the tag "Weapon"
     weaponAnim = weapon.GetComponent<Animator>();

}

#

is the weapon a child of the player

#

beacuse gameobject.find is not really that scalable

lost pivot
#

Player --> weaponparent ---> weapon

vivid saffron
#

okay you could use something like

#

transform.child.child instead of gameobject.find

lost pivot
#

i had no clue you could do .child.child

#

that would work perfectly

vivid saffron
#

well you have to call GetChild(0)

lost pivot
#

ill experiment with that

vivid saffron
#

        //Gets child in child
        foreach (Transform weapon in transform.GetChild(0))
        {
            //gets the animator in the child
            var weaponAnim = weapon.GetComponent<Animator>();

            Debug.Log($"Found weapon " + weapon.gameObject.name);
        }
#

this code makes it possible to have multiple weapons as children

#

great day to you 🙂

lost pivot
#

good day to you aswell, thank you so much