0

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;
       public NavMeshAgent enemyAgent;

  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)

So it throws a null reference on line 29, but then it correctly outputs line 29, and all the agent variables are correctly set in the inspector during play. Is there any way to get rid of the error? Do I just ignore it?

Wolfeius
  • 131
  • 11
  • The answer in the [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142/1260204) is pretty much a "how to troubleshoot a NRE", a guidebook if you will. Use the advice in that answer to figure out what is causing the NRE in your code – Igor May 13 '20 at 14:42
  • I have done that already. This error makes no sense, because it sais null reference, but it does correctly output the line it says there is a null reference on the same frame The line that throws the exception (29) is then run and outputs correctly (29) on the same first frame, all the other variables from the SO are also correctly initialized on the first frame. So it does get initialized correctly, BUT throws the error. – Wolfeius May 13 '20 at 14:48
  • More likely is that your type is instantiated two (or more) times and the first time an exception is thrown and the second time there is no exception thrown. It is not possible in .net for an exception to be thrown but execution to continue to the next LoC without some type of exception handling code (try/catch) – Igor May 13 '20 at 14:53
  • I recommend you try to attach the debugger so you can inspect the application state at the moment the NRE is thrown. – Igor May 13 '20 at 14:54
  • The VS debugger is attached to unity & play, it outputs no error. The error shows in the editor's console. The .asset is assigned in the Unity Inspector and it gets initizalized by Unity Editor. I do not initialized it in the script, nor is the .asset assigned to other game objects in the application. – Wolfeius May 13 '20 at 15:01
  • To be clear, I added the variable to watch and it's value it's not null on the first line of execution in the Start() method, the line before the error appear in the Unity Editor. VS debugger shows no errors. – Wolfeius May 13 '20 at 15:18
  • You should add a guard statement at the top of the method. `if (enemy_1 == null) throw new InvalidOperationException("enemy_1 has not been set, Start can't be called.");` or something similar. This will ensure valid state before the rest of the code executes. – Igor May 13 '20 at 15:21
  • That does not solve the issue. The enemy_1.asset is assigned in the Inspector windows of the editor of that script. Unity should (and does) initialize it. However, before doing so it throws the error, and then everything resumes working as it should, enemy_1.name is correctly printed and all the values for the AI agent are used for the agent on Frame 1, which is when the Start() method runs. I believe this is a problem of the editor itself, as it's the asme they use ont he Unity manual: https://docs.unity3d.com/Manual/class-ScriptableObject.html – Wolfeius May 13 '20 at 15:37
  • SORTED: duplicated script component on the inspector. Nothing to do with debuggers. – Wolfeius May 13 '20 at 15:52

0 Answers0