- Home /
Destroy Prefab clone
Hey everyone..
I am having one coin pattern prefab in that i have collection of coin with single parent object..
Now whenever my player collides with that coin i am destroying coin in player script. Nd for spawning coin i have another spawner script.
Now when i instantiate coin pattern next time, i could not find previously destroyed coin.. means coins are destroying from prefab itself..
So for that what should i do?? Thanks for your support and help till now.
function FixedUpdate() {
var ray : Ray = Ray(player.transform.position, Vector3.up);
var hit : RaycastHit;
var somthingAboveMe : boolean = Physics.Raycast(ray, hit);
if (Physics.Raycast(ray, hit)) {
if(hit.collider.name == "Coin")
{
if(hit.collider.gameObject.transform.parent.name.Contains("(Clone)"))
{
Debug.DrawLine (ray.origin, hit.point);
player.gameObject.rigidbody.velocity = new Vector3(0, 0, 0);
player.gameObject.rigidbody.AddRelativeForce(0 , 800 ,0);
player.gameObject.layer = 8;
ChangeChildLayerToPlayer();
Destroy(hit.collider.gameObject);
}
}
}
}
If you destroy coins you will not be able to use them again later. Try hiding them.
You would only use this SetActive(false) option if you wanted to use that same instance/clone again, if you want to Instantiate another clone ins$$anonymous$$d then just make sure that you're destroying the clone and then you should have no problem creating another instance of the coin using the Instantiate() function.
Answer by Tasmia · Apr 21, 2014 at 10:21 AM
You need to attach Destroy(gameObject); ..with coin if collision occur ... otherwise if you destroy clone it will destroy main gameObject..
Answer by Fappp · Apr 24, 2014 at 08:40 AM
var goHit : GameObject;
function FixedUpdate() {
goHit = null;
var ray : Ray = Ray(player.transform.position, Vector3.up);
var hit : RaycastHit;
var somthingAboveMe : boolean = Physics.Raycast(ray, hit);
if (Physics.Raycast(ray, hit)) {
if(hit.collider.name == "Coin")
{
goHit = hit.collider.gameObject;
Destroy(goHit);
}
}
}
This makes sure the GameObject deleted is always the one raycasted towards and only if the name is Coin!
I would suggest implementing CompareTag instead of searching by name, just for the ease of it!