Cant use this code, I want to switch prefab.
It says!
Can´t add script behaviour Switchprefab. You need to fix all compile errors in all scripts first!
Don´t know what to do about it!
Is this code right?
// Set in the inspector
var prefab1 : GameObject;
var prefab2 : GameObject;
// Prefab to use
private var prefab : GameObject;
function Start ()
{
prefab = prefab1;
}
function Update ()
{
// Use the first prefab when pressing 1 and the second prefab when pressing 2..
if ( Input.GetKeyDown ("1") )
prefab = prefab1;
if ( Input.GetKeyDown ("2") )
prefab = prefab2;
if ( /* Something happens ?? */ )
Instantiate (prefab, transform.position, Quaternion.identity);
}
I think something is wrong here at the end of the code!
if ( /* Something happens ?? */ )
Instantiate (prefab, transform.position, Quaternion.identity);
}
“Something Happens” isn’t a real condition for an if statement 
You need to figure out (and code) what conditions should be met for one of the two prefabs to be instantiated.
something with destroy perhaps?
Does anybody know how to change this code to destroy the prefab after I made any selection. ’
If I hit keybutton 1 I get prefab1 and remove prefab2 and If hit keybutton 2 I get prefab2 and remove prefab1!
Keep a reference to the instantiated object and use Destroy on it before instantiating the new one:-
var currentObject: GameObject;
...
Destroy(currentObject);
currentObject = Instantiate (prefab, transform.position, Quaternion.identity);
1 Like
Hi Andeeee!
Thankyou for helping me!
Where do I put your code in the code below to make it work?
Thankyou in advance!
// Set in the inspector
var prefab1 : GameObject;
var prefab2 : GameObject;
// Prefab to use
private var prefab : GameObject;
function Start ()
{
prefab = prefab1;
}
function Update ()
{
// Use the first prefab when pressing 1 and the second prefab when pressing 2..
if ( Input.GetKeyDown ("1") )
prefab = prefab1;
if ( Input.GetKeyDown ("2") )
prefab = prefab2;
if (Input.GetKeyDown ("1") )
Instantiate (prefab, transform.position, Quaternion.identity);
if (Input.GetKeyDown ("2") )
Instantiate (prefab, transform.position, Quaternion.identity);
}
function OnNetworkLoadedLevel ()
{
Network.Instantiate(prefab, transform.position, transform.rotation, 0);
}
function OnPlayerDisconnected (player : NetworkPlayer)
{
Debug.Log("Server destroying player");
Network.RemoveRPCs(player, 0);
Network.DestroyPlayerObjects(player);
Inside the “if” statements where you currently have:-
Instantiate (prefab, transform.position, Quaternion.identity);
…try replacing it with the code in the previous post.
Hi Andeeee!
Do you mean like this?
It don´t work, It keeps going on and clonenig the character.
// Set in the inspector
var prefab1 : GameObject;
var prefab2 : GameObject;
// Prefab to use
private var prefab : GameObject;
function Start ()
{
prefab = prefab1;
}
function Update ()
{
// Use the first prefab when pressing 1 and the second prefab when pressing 2..
if ( Input.GetKeyDown ("1") )
prefab = prefab1;
if ( Input.GetKeyDown ("2") )
prefab = prefab2;
if (Input.GetKeyDown ("1") )
Destroy(prefab2);
prefab2 = Instantiate (prefab, transform.position, Quaternion.identity);
if (Input.GetKeyDown ("2") )
Destroy(prefab1);
prefab1 = Instantiate (prefab, transform.position, Quaternion.identity);
}
function OnNetworkLoadedLevel ()
{
Network.Instantiate(prefab, transform.position, transform.rotation, 0);
}
function OnPlayerDisconnected (player : NetworkPlayer)
{
Debug.Log("Server destroying player");
Network.RemoveRPCs(player, 0);
Network.DestroyPlayerObjects(player);