#gameobject.Getcomponent<Animator>(); yielding no animator...?
1 messages · Page 1 of 1 (latest)
Having a little issue with:
weapon = GameObject.FindGameObjectWithTag("Weapon");
weaponAnim = weapon.GetComponent<Animator>();
}```
https://prnt.sc/cIm-39AEw8Ju
https://prnt.sc/utxcYlC-ddWD
<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;
///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>();
}```
Well for starters the method should be named Start not start
facepalm
And the weapon should be tagged appropriately
well that worked.
why would you wait one frame?
Thank you lol
doesnt make much sense for me
Np
Waiting one frame because if i dont, the game object doesnt exist for weaponAnim = --> weapon <--- .GetComponent<Animator>();
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
Player --> weaponparent ---> weapon
okay you could use something like
transform.child.child instead of gameobject.find
well you have to call GetChild(0)
was infact not needed, good call tysm
ill experiment with that
//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 🙂