Hi Guys! I am trying to use a Text Type public variable and accessing it in another script to change text color. I am referencing it but still i am getting the error
NullReferenceException: Object reference not set to an instance of an object
Here is the code:
void Start () {
imsiscript = imsi.GetComponent ();
blacklistscript = blacklist.GetComponent ();
}
While entering the trigger it should change the color of text.
void OnTriggerEnter( Collider other)
{
if (counter%2==1)
{
lightGreen.SetActive (true);
imsiscript.txtimsi.color = Color.black;
blacklistscript.textblacklist.color = Color.black;
}
else if (counter%2==0)
{
lightRed.SetActive (true);
imsiscript.txtimsi.color = Color.red;
blacklistscript.textblacklist.color = Color.red;
}
}
Its doing everything fine just giving exception showing NullReferenceException. Please help.
@LeftyRighty Thanks for your response. I have solved the problem one of my script was disabled by me accidentally which was causing the issue. Basically I was enabling and disabling some gameobjects on triggers enter and exit. Mistakenly I put wrong script in the GameObject Reference which causes the issue enabling right script on right game object solves my problem.
no worries, for future reference when you post issues on the forums can you include the line number part of the error, or if you’re posting a snippet of your code indicate which line the error is referring to so we can make sense of it quicker
@LeftyRighty thanks bro I will surely post the line number in future. Thanks again for your response. One more question can you please tell me how to put some answer as right answer which is shown as green in most of the questions in unity forums so that answer can be identified more quickly by others.