Question by
magicskillz · May 29, 2016 at 02:01 PM ·
errordisableenable
Why doesn't enabling and disabling not work?
I am a noob a programer but I can't find how to find a solution to this error:
NullReferenceException: Object reference not set to an instance of an object CTF.Changing_Color.ChangingFlagColor () (at Assets/Scripts/Changing_Color.cs:29) CTF.Changing_Color.Start () (at Assets/Scripts/Changing_Color.cs:16)
My Code:
using UnityEngine;
using System.Collections;
namespace CTF {
public class Changing_Color : MonoBehaviour
{
private RaycastHit hit;
public GameObject Red;
public GameObject Blue;
public GameObject Neutral;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
ChangingFlagColor();
}
void ChangingFlagColor()
{
if(hit.transform.CompareTag("Blue"))
{
Red.SetActive(false);
Blue.SetActive(true);
Neutral.SetActive(false);
}
if (hit.transform.CompareTag("Red"))
{
Red.SetActive(true);
Blue.SetActive(false);
Neutral.SetActive(false);
}
}
}
}
Comment
Best Answer
Answer by Gobaz · May 29, 2016 at 02:08 PM
Since hit is private i know you are not setting it in the inspector. And your not setting it here. But you are using it, there by using an object reference not set to an instance of an object.
Also double check you set the; Red,Blue and Neutral in the inspector.