- Home /
Scriptable Object null reference error, but it works?
I am making an SO with the definition for an Enemy, like this:
public class Enemy_SO : ScriptableObject
{
public string enemyName;
[Header("AI Agent Settings")]
public int speed;
public int angularSpeed;
}
And I am attaching it to the Enemy GameObject like this:
public class EnemyController : MonoBehaviour
{
public Enemy_SO enemy_1;
private void Start()
{
enemyAgent = GetComponent<NavMeshAgent>();
Debug.Log("name: " + enemy_1.name);
enemyAgent.speed = enemy_1.speed;
}
}
On play the console outputs:
NullReferenceException: Object reference not set to an instance of an object
EnemyController.Start () (at Assets/Scripts/Enemies/EnemyController.cs:29)
name: Enemy 1
UnityEngine.Debug:Log(Object)
EnemyController:Start() (at Assets/Scripts/Enemies/EnemyController.cs:29)
Even thou it throws a null reference error, it does print the correct name, and the other definions (agent speed, etc) do get modified and they show up correctly on the inspector while on Play. Why is then this error happening?
I have tried with Awake and Start and the error shows, but prints and modifies the agents as per the SO...
Any help would be greatly appreciated
Your answer
Follow this Question
Related Questions
Unity : Singleton ScriptableObjects resets after play 1 Answer
Need some advice for my inventory system 0 Answers
How to Instantiate objects that are Scriptable Objects 0 Answers
Serializing ScriptableObject into scene without writing to asset 1 Answer
Unique function for each instance of a scriptable object 2 Answers