I am working on an Unity3d project in which I want to dynamically add 3d objects to the scene, at the same position, orientation, upon a keyPress. For doing so, I imported these objects and made a prefab for all of these.. I am using the following script to do so.. bt it doesn't do the needfull.. The new object gets instantiated and gets added to the scene but the old objects don't get destroyed. And also ,the position of the new objects is not always the same. The scene consists of a Cube initially. After I press '1' ,'2' etc .. I want a new Object to replace the 1 being displayed (at the same position).
script :
var myCar : GameObject;
var Cube : GameObject;
var Globe: GameObject;
var Clock : GameObject;
var Pistol : GameObject;
var LCD : GameObject;
var prev : int;
var temp: GameObject;
function Start ()
{
prev =0;
temp=null;
}
function Update ()
{
if(prev==0)
temp=Cube;
else if(prev==1)
temp=myCar;
else if(prev==2)
temp=Globe;
else if(prev==3)
temp=Pistol;
else if(prev==4)
temp=Clock;
else if(prev==5)
temp=LCD;
if(Input.GetKey(KeyCode.Alpha1 || KeyCode.Keypad1))
{
if(prev!=1)
{
Instantiate(myCar,temp.transform.position ,temp.transform.rotation);
myCar.transform.localScale = Vector3(0.06,0.06,0.06);
Destroy(temp);
prev=1;
}
}
else if(Input.GetKey(KeyCode.Alpha2 || KeyCode.Keypad2))
{
if(prev!=2)
{
Instantiate(Globe,temp.transform.position ,temp.transform.rotation);
Globe.transform.localScale = Vector3(0.04,0.04,0.04);
Destroy(temp);
prev=2;
}
}
else if(Input.GetKey(KeyCode.Alpha3 || KeyCode.Keypad3))
{
if(prev!=3)
{
Instantiate(Pistol,temp.transform.position ,temp.transform.rotation);
Pistol.transform.localScale = Vector3(0.03,0.03,0.03);
Destroy(temp);
prev =3;
}
}
else if(Input.GetKey(KeyCode.Alpha4 || KeyCode.Keypad4))
{
if(prev!=4)
{
Instantiate(Clock,temp.transform.position ,temp.transform.rotation);
Clock.transform.localScale = Vector3(0.08,0.08,0.08);
Destroy(temp);
prev=4;
}
}
else if(Input.GetKey(KeyCode.Alpha5 || KeyCode.Keypad5))
{
if(prev!=5)
{
Instantiate(LCD,temp.transform.position ,temp.transform.rotation);
LCD.transform.localScale = Vector3(0.04,0.04,0.04);
Destroy(temp);
prev=5;
}
}
}
There are several things to consider:
Destroy is called on the prefab objects but not on the existing GameObject instance and thus doesn't do anything. transform. They affect the prefabs only.localScale adjusting.Putting this altogether, my suggestions:
Cube initialised on start, you first drag it into the scene, and then drag the newly created Cube from hierarchy view to the varable current. So you see the cube first when starting the scene and your script knows that this is current when it has to replace it.I'm working in C# but it should be something like
var current : GameObject;
...
function ReplaceObject (prefab : GameObject, newKey : int) {
if (newKey != prev) {
GameObject newObject = Instantiate(prefab, current.transform.position,
current.transform.rotation);
Destroy(current);
current = newObject;
prev = newKey;
}
}
...
function Update ()
{
if(Input.GetKey(KeyCode.Alpha1 || KeyCode.Keypad1)) {
ReplaceObject (myCar, 1);
} else if(Input.GetKey(KeyCode.Alpha2 || KeyCode.Keypad2))
ReplaceObject (Globe, 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With