- Home /
Destroy(gameObject) destroys entire scene
The title is self explanatory, i have 5 objects in the scene, 4 capsules and a sphere. When I click on the ball to select it and then click on a container, i would like to destroy the ball, but as it stands, the entire scene is deleted.
Can anyone shed some light on my mistake? Code below:
using UnityEngine;
using System.Collections;
public class selectObject : MonoBehaviour
{
public bool isSelected = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
RaycastHit hitInfo = new RaycastHit ();
bool hit = Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (Input.GetMouseButtonDown (0) && hit)
{
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if (hitInfo.transform.gameObject.tag == "red" && hitInfo.transform.gameObject.name == "Ball")
{
isSelected = true;
}
if(isSelected == true)
{
if (hitInfo.transform.gameObject.name == "Capsule")
{
Debug.Log ("I'm here");
Destroy (gameObject);
}
}
}
}
//check if object is selected, if it is, click on container
//you want it to go in, if isSelected == true and the colour tag
//matches the container tag, destroy object and award score
}
You really really don't want to do the ray cast every frame. $$anonymous$$ove the raycast down to inside the input if.
Answer by Kiwasi · Jul 19, 2014 at 09:03 PM
Is this script on every gameObject in the scene?
If so that's your problem. This script destroys the gameObject it is on when the conditions are met.
Argh, I've been at this too long, i had it on my camera when i meant to have it on my ball object, thanks for the help
Your answer
Follow this Question
Related Questions
Creating and destroying password stage between scenes 0 Answers
Object Reference not set to an instance of an object only appears occasionally? 1 Answer
How to make a saved score destroy on load new scene 0 Answers
Make music play across scenes but destroy when it returns to beginning scene. 1 Answer