- Home /
Spawn object every 5 coins collected
Hello,
I am trying to write a script to spawn a game object for every 5 coins collected.
I have the spawn working when I collect the first 5 coins. The problem is as long as coinsCollected = 5 then I get multiple spawns because start Spawn code is in Update. As soon as I collect another coin then coinsCollected does not = 5 so the spawning stops. How do I ad a variable or int for C# so the spawing stops immediately.
Also how would I make my script work so that every 5 coins collected a game object will spawn. The game object spawning is always going to be the same game object(like a power up).
Thanks in Advance.
Here is my code:
public GameObject obj;
public Vector3 spawnValues;
private PlayerMove playerMove;
private GUIManager gui;
int myInt = 1;
void Start ()
{
playerMove = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMove>();
gui = FindObjectOfType(typeof(GUIManager)) as GUIManager ;
transform.position = playerMove.transform.position;
}
void Update ()
{
if (gui.coinsCollected == 5){
Spawn();
}
}
void Spawn ()
{
Instantiate(obj, transform.position, Quaternion.Euler(Vector3.zero));
//Instantiate
}
}
Hello, That doesn’t work because the coins collected are a score in itself. The spawned object per 5 coins collected is like a bonus. So I can’t keep resetting the coin collected score back to 0.
Your scripting did give me an idea though for a hidden secondary value attached to the coins that can be reset when 5 have been collected. What do you think? I welcome any feedback even obvious ones.
Thanks
The hidden value will work fine, just add 1 to the hidden score and the real score when collecting a coin. But I would recommend checking each time you add a new coin ins$$anonymous$$d of checking in each update. Then you have greater control and you could skip using the hidden value altogether by using the modulus operator to check if the value is divisible by 5:
Example:
if(coinsCollected % 5 == 0)
{
Spawn();
}
I appreciate your help but when I tried your script my game object was spawning every time I collected a coin. Aren't all values divisible by 5 becuase it will be just broken down into a fraction? For example: 1/5 = 0.2
Does the modulus operator check for whole numbers only?
I did set up the hidden value scenario and it works but yours is simpler if it worked like I need it to.
Thanks
The modulus operator returns the remainder of the division, so for example 14%5 = 4 because 14 = 2*5 + 4. Testing if the remainder equals 0 is the same as testing if it is a multiple of the number you divide by.
It works fine on integers (whole numbers). coinsCollected should be an integer anyway.
Answer by c-sprong · Nov 25, 2014 at 09:48 AM
You could call Spawn() just after you increase coinsCollected, instead of in the Update function. That way it would only spawn one object every five coins. To make it spawn every five coins you can use a hidden variable that you reset after collecting 5, or do:
if ((gui.coinsCollected % 5) == 0){ // Remainder of a division by 5 equal to 0 means it is true on multiples of 5
}
Thanks for your help. I will try your suggestions and let your know how it goes. I need to restructure my code a little so I don't have to use update.
hey buddy, did you already checked my answer? that way you don't need to restructure anything and it works as you need to
Answer by f4bo · Nov 25, 2014 at 08:39 PM
just change yourcode like this:
int nextSpawn = 5;
...
void Update ()
{
if (gui.coinsCollected == nextSpawn){
Spawn();
nextSpawn+=5;
}
}
That works, but I think events should really be handled as they occur, not polled for every frame.
Also, something like
while (gui.coinsCollected>=nextSpawn){
Spawn();
nextSpawn+=5;
}
would be more robust.
my code works just as intended - it calls spawn every 5 coin, there really is no need to complicate things further. If you want to have it like an event - which make better sense - then you have to use the Ontrigger function not the Update and put that check there.
Your code may fail if the player can pick up multiple coins in the same frame, which is why I suggested an alternative.
to help you out better then, there is something I would know more about your code though: where is this script attached to and where and how it is updated gui.coinsCollected?
Your answer
Follow this Question
Related Questions
Spawn different Player models 0 Answers
player spawning 0 Answers
Camera cannot find instantiated player 1 Answer
Add spawn to script [Multiplayer] 0 Answers
how to make a character resume after going into a random battle? 1 Answer