- Home /
Problem is not reproducible or outdated
Problem with finding animator.
So i have a weapon that gets Instantiated into the scene and it need a animator. I tried to use this code but it wouldn't work.
var TheAnimator : Animator;
function Awake ()
{
var TheAnimator : Animator = gameObject.GetComponent("Animator") as Animator;
}
I need to the script to find it automatically or I wont be able to make it work.
Answer by tanoshimi · Sep 18, 2016 at 10:40 AM
You've already declared TheAnimator at the top of your class - so don't redeclare it as a local variable within Awake() as well. Also, I'd recommend you always use the generic version of GetComponent to avoid the need for casting.
var TheAnimator : Animator;
function Awake ()
{
TheAnimator = GetComponent.<Animator>();
}
Alternatively, is there a reason why you don't just drag the Animator component into the TheAnimator slot in the inspector?
The animator is on another Game Object but the script is on a object which is instantiated in the scene
Follow this Question
Related Questions
Trouble working with a Custom class, JavaScript. 2 Answers
find GUIText 1 Answer
How to Instantiate prefab as child? (Java) 1 Answer
Prevent instantiating on collider 1 Answer