- Home /
Duplication Problem. Please help.
Hello. I am fairly new to both Game Creation and Scripting. I had created a script that duplicated my set object. I also had another script that destroys the object when the player collides with the object. All goes well until the duplication part. When the object becomes duplicated it changes its name from "Apples" to "Apples(clone)" When it adds the "(clone) to the end it doesn't allow my destroy on collision script to work since the name isn't "Apples" This is the script using UnityEngine; using System.Collections;
public class DestroyApples : MonoBehaviour {
void OnCollisionEnter2D (Collision2D col)
{
//Check collision name
Debug.Log("collision name = " + col.gameObject.name);
if(col.gameObject.name == "Apples")
{
Destroy(col.gameObject);
}
}
}
Is there a way it can be duplicated without the "(clone)" or a way that it allows the script to detect the object.
Sorry if my writing is in poor condition. I am quite tired. If any questions pop up, please ask.
All help is very much appreciated. Thank you.
Answer by alok-kr-029 · Feb 28, 2015 at 05:48 AM
try to store the gameobject in a local variable
eg -
Gameobject gObj = (Your Instantiate code )as GameObject;
gObj.transform.name ="Apples";
This will work
Thank you for the reply. So where you have said (Your Instantiate Code) I would put what in there?
In the place you call instantiate to duplicate your object, ins$$anonymous$$d of using
Instantiate(something, byThere, thatWayRound);
use
GameObject myClone = Instantiate(something, byThere, thatWayRound);
Then you have a reference to the instantiated object, with which you can refer to its name and change it, as stated above.
@alok, the .transform. is an unnecessary step. gObj.name is fine.
Answer by maccabbe · Feb 28, 2015 at 06:37 AM
You can also destroy based on the tag of the enemy. Set the tag of the apple prefab to a new tag, "Apples" then use the following
if(col.gameObject.tag== "Apples"){
Destroy(col.gameObject);
}
Or try too the layer to destoy it
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.layer == 9)
Destroy(coll.gameObject);
}
Your answer
Follow this Question
Related Questions
Transform.localPosition Script Problem. 3 Answers
Collision On Object Error. Please help. 1 Answer
Problem creating a script to destroy object on collision. 1 Answer
Transform.localPosition Scripting Problem. 1 Answer
Expand object on touch? 1 Answer