I’m very new to unity rusty as hell with javascript.
I have two gui buttons that instantiate 2 different 3d models (ItemOne ItemTwo) that are defined in the variable “targetShape”, but I’m having trouble destroying (removing) the previous 3d model first from the scene.
The script Instatiates the models in the position of ItemLocater, but the objects it creates are named as “ItemOne(Clone)” or “ItemTwo(Clone)”
Can anyone shed any light for me as the objects don’t destroy.
My code so far.
var normalTexture : Texture;
var hoverTexture : Texture;
var pressedTexture : Texture;
var targetShape : GameObject;
private var currentShape : GameObject;
var selectOne: boolean;
private var state = 0;
private var myGUITexture : GUITexture;
myGUITexture = GetComponent(GUITexture);
function OnMouseEnter()
{
state++;
if (state == 1)
myGUITexture.texture = hoverTexture;
}
function OnMouseDown()
{
state++;
if (state == 2)
myGUITexture.texture = pressedTexture;
var go = GameObject(“ItemTwo(Clone”);
Destroy(go);
AddMyObject();
}
function OnMouseUp()
{
if (state == 2)
{
state–;
}
else
{
state --;
if (state < 0)
state = 0;
}
myGUITexture.texture = normalTexture;
}
function OnMouseExit()
{
if (state > 0)
state–;
if (state == 0)
myGUITexture.texture = normalTexture;
}
function AddMyObject()
{
var MyItem : Rigidbody = Instantiate((targetShape),gameObject.Find(“ItemLocater”).transform.position,Quaternion.identity);
}
Didn’t look too hard into the code (so I don’t know if there are logical/syntax bugs), but I did notice that on this line:
var go = GameObject(“ItemTwo(Clone”);
You’re missing the second parenthesis in “Clone” (should be “ItemTwo(Clone**)**”)
Sorry, should have done a fresh paste from my code rather than amending it (badly) in the email. I already have it as you corrected, so its another problem.
Seems to be a logic problem. I’m trying to instantiate and destroy all from the same button script with no scripts on the prefab.
If I simplify it down, the critical part of my button script is this, but its not destroying the clones. I can make it destroy a pre existing object if its not been Instantiated
function OnMouseDown()
{
/* destroys the last clone*/
var go = GameObject(“ItemTwo(Clone)”);
Destroy(go);
/* Creates the next clone*/
AddMyObject();
}
function AddMyObject()
{
var MyItem : GameObject = Instantiate((targetShape),gameObject.Find(“ItemLocater”).transform.position,Quaternion.identity);
}
Maybe if I make a “Kill” function with the Destroy() in it and put it in my instantiated prefab’s script then how do I invoke it from the button script. I’ve had no luck doing this so far?
Ow! Not having any luck destroying an instantiated object. My logic/ syntax must be at fault.
Can anyone point me to an example where they are Instantiating and then Destroying an object all from one piece of code( on a GUI button for example). I’m trying to destroy the old object if its there, so I can instantiate something else in the same location.
All of the Destroy code I’ve seen involves putting the Destroy on the actual object/ prefab your making and having it destroy itself if some criteria is met (time, distance traveled, etc) but I’m using static models here, all controlled from the GUI button script I’m writing.
Simplifying what little I have for this part goes as follows
var MyItem : GameObject;
function OnMouseDown()
{
AddMyObject();
Destroy (MyItem));
}
function AddMyObject()
{
var MyItem : GameObject = Instantiate((targetShape),GameObject.Find("ItemLocater").transform.position,Quaternion.identity);
MyItem.name = "theShape";
}
I get console error “Destroying assets is not permitted to avoid data loss.”
Any suggestions gratefully received.
You are using a local variable to hold the reference to the instantiated object and it has the same name as the script variable MyItem. The local variable is not the same item as the script variable and it ceases to exist after the function returns. You need to declare another script variable (not called MyItem
) and use that to hold the reference to the instantiated object. This is also the variable you should pass to the Destroy function.