- Home /
GameObject clones won't add a point.
Alright, i have a simple 2d game, where you click a button it should add a point. So i have a random spawner at the top, which is just ping poning back and fourth dropping an object. I just want to click the gameobject and add a point. Now i can do that once, then after i click the object once i get this error.
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Object.Instantiate (UnityEngine.Object original) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:91) SpawnMultipleObjects.SpawnRandomObject () (at Assets/Scripts/SpawnMultipleObjects.cs:32) SpawnMultipleObjects.Update () (at Assets/Scripts/SpawnMultipleObjects.cs:52)
Okay now here is my 2 scripts i have, the one that spawns the objects. Then the other one that Destroys the object and adds a point. Like i said, all i want is to know what i need to do, to make it not say this error and when i click on the gameobject clones it adds a point.
using UnityEngine; using System.Collections;
public class SpawnMultipleObjects : MonoBehaviour { private Vector3 startPosition; private float newXPos = 0f; public float moveSpeed = 1f; public float moveDistance = 4f; public GameObject[] gameObjectSet; public float timeLeftUntilSpawn = 0f; public float startTime = 0f; public float secondsBetweenSpawn = 3f; // Use this for initialization void Start () { startPosition = transform.position; } void SpawnRandomObject() { int whichItem = Random.Range (0, 2); Debug.Log ("Our random number is " + whichItem); GameObject myObj = Instantiate (gameObjectSet [whichItem]) as GameObject; myObj.transform.position = transform.position; } // Update is called once per frame void Update () { // newXPos++; newXPos = Mathf.PingPong(Time.time * moveSpeed, moveDistance) - (moveDistance / 2f) ; transform.position = new Vector3 (newXPos, startPosition.y, startPosition.z); timeLeftUntilSpawn = Time.time - startTime; if (timeLeftUntilSpawn >= secondsBetweenSpawn) { startTime = Time.time - Random.Range (0.1f, 0.5f); timeLeftUntilSpawn = 0; // Debug.Log ("Spawn one here"): SpawnRandomObject(); } } }
=================================================================================================
using UnityEngine;
using System.Collections;
public class DestroyOnTouch : MonoBehaviour {
int myScore = 0;
public GUIText scoreText;
public void OnMouseDown () {//Work with touch
Destroy(gameObject);
myScore = myScore + 1;
}
public void OnDestroy () {
scoreText.text = "Score: " + myScore;
}
}
Your answer
Follow this Question
Related Questions
Trying to make a Point system that references a Prefab. 1 Answer
Multiple Cars not working 1 Answer
How can I make the script to play with the mouse? 0 Answers
Distribute terrain in zones 3 Answers
Random Player Respawn Points 3 Answers