NullReferenceException: Object reference not set to an instance of an objectInt32)
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)
Answer by Jessespike · Jul 06, 2016 at 07:00 PM
As you're aware, Check4Cubes has a for-each loop iterating through prefabs in "so.prefabs".
So I have to ask, does the cube have a SpawnObjects component attached? And does the prefabs array contain objects? Because that seems to be what the error is referring to.
Although, it seems funny that each spawned object will contain another SpawnObjects script. Intended? Usually the way is to have one ObjectPoolManager or SpawnManager (SpawnObjects) and the objects are independent. Not each object containing another spawn manager. The simplest solution is to remove the Check4Cube function, you can assume the object being hit is the one calling OnMouseDown.
void OnMouseDown()
{
Destroy (this.gameObject);
Instantiate (exPrefab, transform.position, Quaternion.identity);
//Check4Cube();
Debug.Log("Cube hit");
}
yes prefabs array contain Object:
using UnityEngine;
using System.Collections;
public class SpawnObjects : $$anonymous$$onoBehaviour {
public GameObject[] prefabs;
private float nextPrefabTime=0.0f;
private float spawnRate=1.5f;
public float timeLeft;
int score=0;
public Camera cam;
// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera. main;
}
Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
}
// Update is called once per frame
void Update () {
if (nextPrefabTime < Time.time) {
StartCoroutine (Spawn ());
nextPrefabTime = Time.time + spawnRate;
//speed up Spawn Rate
spawnRate *= 0.9f;
spawnRate = $$anonymous$$athf.Clamp (spawnRate, 0.3f, 99f);
}
}
public IEnumerator Spawn()
{
yield return new WaitForSeconds (1.0f);
GameObject pre = prefabs [Random.Range (0, prefabs.Length)];
Vector3 spawnPosition = new Vector3 (transform.position.x + Random.Range (-11f, 11f), transform.position.y, transform.position.z);
Instantiate (pre, spawnPosition, Quaternion.identity);
}
}
Your answer
Follow this Question
Related Questions
Move the character automaticly with the road 0 Answers
adjust image size with mouse 0 Answers
Setting Hierarchical slider parameters via script 0 Answers
WebCamTextures square 0 Answers
List does not contain a definition for Except error. 2 Answers