- Home /
Instantiate a prefab and then add a animator component and controller to the component.
i keep getting the error The name `playertransform' does not exist in the current context - i imagine its a simple answer but im still learning.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Instantiationship : MonoBehaviour
{
// Reference to the Prefab. Drag a Prefab into this field in the Inspector.
public GameObject myPrefab;
// This script will simply instantiate the Prefab when the game starts.
void Start()
{
// Instantiate at position (0, 0, 0) and zero rotation.
Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
Animator animator = playerTransform.gameObject.GetComponent<animator>();
animator.runtimeAnimatorController = Resources.Load("Assets\\scenetest\\scenetest2\\acship1.controller") as RuntimeAnimatorController;
}
}
Answer by IINovaII · Jan 05, 2020 at 11:17 AM
The reason why you are getting that error is because you haven't declared playerTransform
variable anywhere in that script. There are lot of other mistakes in that code. I'll show you the corrected code and more information to it right below it.
void Start()
{
Gameobject newGameObject = Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
Animator animator = newGameObject.AddComponent<Animator>();
animator.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("LocationOfAnimatorController/NameofAnimatorController");
}
Instantiation of new GameObject
Gameobject newGameObject = Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
When you instantiate it this way, you'll keep a reference of the instantiated object in the newGameObject variable which you can use to addcomponents or do other things with it right away.
Adding a new component to Gameobject
Use AddComponent instead of GetComponent when you want to add a component. You have to type in componenttype inside instead of variable name in order for it to work or it won't compile.
Loading Assets using Resource.Load
This function will only load assets from Resources folder and NOT anything from Assets folder. You have to put anything and everything that you want to load like this to the Resources folder. If this folder is not inside your Assets folder, you have to manually create one.
Don't include the extension part of the asset that you are loading this way which means in your case, remove .controller part. You are only required to add name of the asset there.
i get this error Object reference not set to an instance of an object Instantiationship.Start () (at Assets/Resources/Instantiationship.cs:15)
the thing is though it does exist its line 15 in script which is Resources.Load("Assets/Resources/acship1");
Answer by LOSTSOUL86 · Jan 05, 2020 at 10:59 AM
Hi, but do you want to add new component animator to the prefab?
Is that your question?
if you want to add blank animator it should be:
Animator animator = myPrefab.AddComponent<Animator>();
and then you can just refere to animator...
or just:
myPrefab.AddComponent<Animator>();
and later
Animator animator = myPrefab.GetComponent<Animator>();
The problem with your code is that playerTransform variable does not exist? What is playerTransform what do you want to do in this line? If you want to get animator that is assignet to the gameObject with "Instantiationship" class assigned then it will be...
Animator animator = transform.GetComponent<Animator>();
But it has nothing to do with the prefab.