what am i doing wrong?
i allways get this error when i use this script NullReferenceException: Object reference not set to an instance of an object Ruins.OnTriggerEnter (UnityEngine.Collider box) (at Assets/Scripts/Ruins.cs:24)
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class Ruins : MonoBehaviour { public Text someText; public GameObject AreaEnterSign; public Text someText2;
void Start()
{
someText = GetComponent<Text>();
AreaEnterSign.gameObject.SetActive(false);
someText2 = GetComponent<Text>();
}
void Update()
{
}
public void OnTriggerEnter(Collider box)
{
someText.text = "Ruins Of Suboth";
AreaEnterSign.gameObject.SetActive(true);
someText2.text = "Ruins Of Suboth";
}
public void OnTriggerExit(Collider box)
{
someText2.text = "Hills";
}
}
can someone help me? i am a very very basic understander of c#
Answer by Commoble · Apr 15, 2017 at 03:35 PM
This is a good explanation of what a Null Reference Exception is and how to deal with it.
The gist of it is that you're trying to access someObject.someVariableOrFunction when someObject == null, which throws a null reference exception. To fix it, you need to figure out which object field is null when it shouldn't be, and then either prevent it from being null, or handle what happens when it is null.
In your case, it looks like either you don't have a Text component attached to the game object, or you don't have a gameobject linked to your AreaEnterSign field in the inspector. Checking these out should solve your problem.
Side note: If you have two Text components on your object (or any two components of the same type), you won't be able to reach both of them with GetComponent. I suggest linking them in the inspector to make sure the correct text components are assigned to the correct fields.
ok my text is the problem i have checked and it goes null on playmode
Your answer
Follow this Question
Related Questions
Pass a reference to a button that opened the menu this script is on 0 Answers
How to get component value without using GetComponent() and Update()/FixedUpdate()? 1 Answer
script delay ("on mouseclick") 0 Answers
Using spawners script variable to set relevant speed of an Enemy... Im bewildered 0 Answers
Script to make specific parts of my object bright ? 0 Answers