Question by
Jojoba007 · Feb 21, 2017 at 02:30 PM ·
instantiatespawnscorepoints
spawn object every 10 score points
Hi, I'm new to scripting. I would like to know how I can instantiate an object every 10 score points?
Comment
Best Answer
Answer by Jan_Sch · Feb 21, 2017 at 02:39 PM
This should work:
gameObject obj;
int score;
int oldScore;
if (score == oldScore + 10)
{
Instantiate(obj);
oldScore = score;
}
@Tuckrex
Thanks, I got this code, but it doens't seem to work after I reach a score of 10.
Scoremanager.instance.GetScore() holds the score from Score$$anonymous$$anager.
using UnityEngine;
using System.Collections;
public class CoinFlight$$anonymous$$anager : $$anonymous$$onoBehaviour {
public GameObject coinFlightPrefab;
private int oldScore;
//If you ever need the coin to stop spawning, by doing: CoinFlight$$anonymous$$anager.spawnCoin = false;
public static bool spawnCoin = true;
void Start () {
}
public void SpawnCoin ()
{
if (spawnCoin == true) {
if (Score$$anonymous$$anager.instance.GetScore () == oldScore + 10) {
Instantiate (coinFlightPrefab);
oldScore = Score$$anonymous$$anager.instance.GetScore ();
}
}
}
}
Ah, ok. $$anonymous$$akes sense, sorry I didn't test it :)
@Tuckrex I think you understand me wrong, I used your code in my script as seen above, but it doesn't seem to work. Could you maybe see what I did wrong? Do I still need to call it in start or update or something?