- Home /
Script won't destroy prefab clones...
Hello ,I need some help making this script work :
#pragma strict
function OnMouseDown () {
Destroy (gameObject);
}
Basicly what the script does is that it destroys the objects that the player clicks on.I also got another object which has a script attached to it,which makes it spawn other objects.
The script clones the objects and makes it spawn multiple times.I attached the first script to the prefab but when I click on it,it won't be destroyed.If I click on the original prefab it works,but not on the clones created by the other script.
Anyone has any idea ?
Thank you.
Edit#1 :
I belive the problem doesn't stand at the "destroyer" script,because the Debug.Log I added OnMouseDown won't show up.
My spawner script :
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 2f;
// Use this for initialization
void Start () {
Spawn ();
}
void Spawn()
{
Instantiate(obj[Random.Range(0, obj.GetLength(0))],transform.position, Quaternion.identity);
Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
}
}
BIG UPDATE #1(fix) : I removed the background (2D quad with a texture behind the objects) and all the scripts seem to be working fine,no idea why.Thanks to everyone who tried to help,I realy appreciate it.Such a nice community!
Thank you.
When the game is running, select a clone in the hierarchy and look at the inspector pane - does it definitely have this script attached to it?
On$$anonymous$$ouseDown() requires the object to be the first collider hit. Any chance there is another collider between the mouse and the clicked-on object?
@tanoshimi - Yes,it does have the script attached to it. @robertbu - The cloned object has only a circle collider (2D) attached to it and there isn't anything between the mouse and the object that's being clicked on.
The clones that appear on the hierarchy are white,which means that they don't appear as the prefab,I don't know if that matters.
Answer by PommPoirAbricot · Jun 17, 2014 at 12:09 PM
Try that also
GameObject[] all = GameObject.FindAllGameObjects("yourGameObjectName" + "(Clone)");
foreach(var g in all)
Destroy(g);
I believe, this will destroy all game objects that are clone of the prefab and not the only one which is clicked by the user. Later is the requirement of the OP.
I would guess so,also unity won't recognise FindAllGameObjects as a definition.
This is the error it gives me : Assets/Scripts/BalloonD.cs(7,39): error CS0117: UnityEngine.GameObject' does not contain a definition for
FindAllGameObjects'
And this is the complete script (Which I may have edited it wrong) :
using UnityEngine;
using System.Collections;
public class BalloonD : $$anonymous$$onoBehaviour {
GameObject[] all = GameObject.FindAllGameObjects("yourGameObjectName" + "(Clone)");
void On$$anonymous$$ouseDown () {
foreach(var g in all)
Destroy(g);
}
}
I also added a Debug.Log in my js version of this and I figured out that the function is not being called on click.
ok try that
using UnityEngine;
using System.Collections;
using System.Linq;
public class BalloonD : $$anonymous$$onoBehaviour
{
private GameObject[] all;
// Use this for initialization
void Start()
{
all = GameObject.FindObjectsOfType<GameObject>().Where<GameObject>(g => g.name.Contains("yourGameObjectName" + "(Clone)")).ToArray();
}
void On$$anonymous$$ouseDown()
{
foreach (var g in all)
Destroy(g);
}
}
No errors now,I attached a Debug.Log to it and noticed that it's not being called,I think the real issue is at my spawner script,at the way they're being spawned.I also added some information to the question.
Otherwise if you only want to delete one clicked cloned gameobject, try when intantiating your GameObject, to attach a collider component and this script:
public class BalloonD : $$anonymous$$onoBehaviour
{
void On$$anonymous$$ouseDown()
{
Destroy(this.gameObject);
}
}
Use this script to instantiate :
public class patatipatata : $$anonymous$$onoBehaviour
{
public GameObject myGameObjectToClone;
void OnGUi()
{
if (GUILayout.Button("clone me"))
{
GameObject tmp = Instantiate(myGameObjectToClone, Vector3.zero, Quaternion.identity) as GameObject;
tmp.AddComponent<Collider>();
tmp.AddComponent<BalloonD>();
}
}
}
Your answer
Follow this Question
Related Questions
2D C# destroy a GameObject on collision 2 Answers
2D games; Javascript or C#? 1 Answer
Erasing a gameobject if you have the coordinates. 3 Answers