- Home /
spawns without controller-: Object reference not set to an instance of an object--but i swear it is!
im new and im prepared to feel dumb but i did hours of this and i dont know. the prefab spawns, it has the animator component, but no controller in it. its in resources and resources folder is in assets. 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()
{
GameObject newGameObject = Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
Animator animator = newGameObject.AddComponent<Animator>();
animator.runtimeAnimatorController = Resources.Load("/acship1") as RuntimeAnimatorController;
}
}
Answer by SuperKael · Jan 07, 2020 at 09:25 PM
Based purely on the information and code you have given, the only reason that could would produce a NullReferenceException would be if you didn't set myPrefab in the Inspector. As such, assuming you did indeed do that, I'm gonna need more information. For example, where is this NullReferenceException occuring? It's also worth noting that both "Assets" and "Resources" should start with capital letters, and when using Resources.Load you shouldn't start with a '/'. Just "acship1" would be fine.
Your answer
Follow this Question
Related Questions
[SOLVED]How to set the animator's controller in an editor script? 0 Answers
Instantiate a prefab and then add a animator component and controller to the component. 2 Answers
Animations on Instantiated Prefabs not working 0 Answers
State could not be found, despite having a State with that name. 1 Answer
2 problems with new animation / animator,2 problems with animation 0 Answers