- Home /
Issue was solved by reimporting assets.
Null reference exception with static method
My static method that i've set within an abstract class using a custom namespace keeps giving me a null reference exception when i try to pass a Vector3 to it.
using UnityEngine;
namespace DebugUtility
{
public class DebugUtility
{
public static void DisplayInfo(Vector3 pos, string info)
{
Vector3 pointOnScreen = Camera.main.WorldToScreenPoint(pos);
//rest of the code
}
}
}
I call it with: DebugUtility.DebugUtility.DisplayInfo(Vector3.zero,text);
I just tried that exact code and it works for me. No null reference exceptions... I used a hard-coded string for your 'text' variable though, but I don't see why that would make a difference. What line of code is the error pointing at?
Answer by Bunny83 · Jul 16, 2018 at 08:57 AM
Well, possible reasons are:
The first enabled camera tagged "MainCamera" (Read Only).
The primary Camera in the scene. Returns null if there is no such camera in the scene. This property uses FindGameObjectsWithTag internally and doesn't cache the result. It is advised to cache the return value of Camera.main if it is used multiple times per frame.
The second possible reason is this
//rest of the code
I think you should be more specific here. If you get a NullRef exception you get a stacktrace with line numbers. You haven't provided any. Finally from the code we see it's also possible that your info string is null. However we do not know if and how you may use it inside your method and we do not know where text
is coming from.
In short:
This question can not be answered.
Answer by J-F · Jul 16, 2018 at 09:29 AM
I have a main camera tagged as the default mainCamera. I don't know what the issue here was, but i reimported all assets and its seems to have fixed the issue.