My basic aim in this project when I hit on cube my console should say : Cube Hit
using UnityEngine;
using System.Collections;
public class rotation : MonoBehaviour {
public float speed =3f;
public GameObject exPrefab;
SpawnObjects so;
// Use this for initialization
void Start () {
so = GetComponent<SpawnObjects> ();
}
// Update is called once per frame
void Update () {
transform.position -= new Vector3 (0.0f, speed, 0.0f) *Time.deltaTime;
transform.Rotate (new Vector3 (15,30, 45) * Time.deltaTime);
}
void OnMouseDown()
{
Destroy (this.gameObject);
Instantiate (exPrefab, transform.position, Quaternion.identity);
Check4Cube();
}
void Check4Cube()
{
foreach (GameObject go in so.prefabs) {
if (go.tag == "Cube") {
Debug.Log ("Cube hit");
} else {
Debug.Log ("NO");
}
}
}
}
Error :
NullReferenceException: Object reference not set to an instance of an object
rotation.Check4Cube () (at Assets/Assets/Scripts/rotation.cs:32)
rotation.OnMouseDown () (at Assets/Assets/Scripts/rotation.cs:25)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)