- Home /
Question by
heftyashish · Jul 16, 2012 at 01:16 PM ·
c#prefabnull reference exception
Null reference exception
public class paint : MonoBehaviour {
public float amtToMove;
void Update () {
amtToMove=Time.deltaTime*10;
transform.Translate(-Vector3.forward * amtToMove);
}
}
In my paint class i have the behaviour of the paint object.
public class car : MonoBehaviour {
public GameObject paint;
public Transform paintPrefav;
void Start () {
paint = GameObject.FindGameObjectWithTag("paint");
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
Instantiate(paintPrefav, new Vector3(-5.3f, 26.7f, 20.97f), paint.transform.rotation);
}
In the class car, I instantiate the prefab. Drag and drop on the unity interface is done. After all this, i am getting Null reference exception on the line where I instantiate the Prefab. Please tell me where am i going wrong. Thanks in advance.
Comment
check gameobject tag "paint" and check Has paintPrefav a gameobject?
Answer by delstrega · Jul 16, 2012 at 01:33 PM
If paintPrefav is really set per drag and drop then the only thing that might go wrong is that it can't find an object with a tag "paint". Just add
if(paint == null){
Debug.Log("Paint not found");
}
to your Start() to find out.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Setting an object from a pool active not working 1 Answer
Multiple Cars not working 1 Answer
Prefab Variables Problem 0 Answers