- Home /
Other
How to instantiate a prefab without showing it straight away
I am trying to create something similar as Mario Kart.
So when picking up the powerup (for let's say the green shield) I want to create it so the player can see he has the green shield. But I don't want to fire it yet.
My code:
private void PickUp(PowerUp item)
{
if (Weapons == 1 && weapons[0] == null)
{
weapons[0] = item;
}
}
// in Update
if (Input.GetKeyDown(KeyCode.F))
{
if (Weapons == 1 && weapons[0] != null)
{
Instantiate(weapons[0], transform.forward, transform.rotation);
weapons[0] = null;
}
}
As you can see I want to create it in a way all PowerUps can be instantiated and eventually karts might have multiple weapons.
What is the best way to approach this?
Do I need to Instantiate it beforehand and just not render it and put scripts on inactive?
Answer by donaldleowjj · Oct 03, 2014 at 09:48 AM
Hmm...I'm not too sure whether this will work or not, but how about storing the power ups in a array?
Like this:
public GameObject[] PowerUps;
Then whenever you fire them just delete the powerup from the array or something. Hope it helps?
Well the powerups that a kart has will be stored in an array, however people need to pick those up. So before people picking them up they have nothing, thus the array is empty.
It's not really about storing the powerups. It's how to get them in the array after they picked them up... Should I Instantiate there or later and if later how do I add it to the array here?
Ah I see. Weelll...I would Instantiate the objects on the course first, and when the player touches the object, then before deleting the object off the course, I do a (add to array) function to the power-ups array.
Ye the only thing that might make that hard is that when a player hits a power up box on track it needs to randomly select a power up. This way players that have played the game will not have an advantage by knowing what power up is in what box.