Search Unity

Spawn object every 10 score points

Discussion in 'Scripting' started by Jojoba007, Feb 22, 2017.

  1. Jojoba007

    Jojoba007

    Joined:
    Feb 11, 2017
    Posts:
    148
    I'm trying to get the following script working, I think I miss something simple but I don't see what.

    It's about spawning an object after every 10 score points.

    Scoremanager.instance.GetScore() returns an int with the score.


    CoinManager:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class CoinFlightManager : MonoBehaviour {
    4.  
    5.  
    6.     public GameObject coinFlightPrefab;
    7.  
    8.  
    9.     private int oldScore;
    10.     //If you ever need the coin to stop spawning, by doing: CoinFlightManager.spawnCoin = false;
    11.     public static bool spawnCoin = true;
    12.  
    13.     void Start () {
    14.  
    15.     }
    16.  
    17.     public void SpawnCoin ()
    18.     {
    19.         if (spawnCoin == true) {
    20.  
    21.                 if (ScoreManager.instance.GetScore () == oldScore + 10) {
    22.                     Instantiate (coinFlightPrefab);
    23.                     oldScore = ScoreManager.instance.GetScore ();
    24.                 }
    25.         }
    26.     }
    27. }
    CoinScript (on the coin itself):


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class CoinScript : MonoBehaviour {
    4.     //Set these variables to whatever you want the slowest and fastest speed for the coins to be, through the inspector.
    5.     //If you don't want coins to have randomized speed, just set both of these to the same number.
    6.     //For Example, I have these set to 2 and 5
    7.     public float minSpeed;
    8.     public float maxSpeed;
    9.     //Set these variables to the lowest and highest y values you want coins to spawn at.
    10.     //For Example, I have these set to 1 and 4
    11.     public float minY;
    12.     public float maxY;
    13.     //Set this variable to how far off screen you want the coins to spawn, and how far off the screen you want the coins to be for it to despawn. You probably want this value to be greater than or equal to half the width of your coins.
    14.     //For Example, I have this set to 4, which should be more than enough for any coins.
    15.     public float buffer;
    16.     float speed;
    17.     float camWidth;
    18.     void Start() {
    19.         //Set camWidth. Will be used later to check whether or not coin is off screen.
    20.         camWidth = Camera.main.orthographicSize * Camera.main.aspect;
    21.         //Set Coin Movement Speed, and Position to random values within range defined above
    22.         speed = Random.Range(minSpeed, maxSpeed);
    23.         transform.position = new Vector3(-camWidth - buffer, Random.Range(minY, maxY), transform.position.z);
    24.     }
    25.     // Update is called once per frame
    26.     void Update() {
    27.         //Translates the cloud to the right at the speed that is selected
    28.         transform.Translate(speed * Time.deltaTime, 0, 0);
    29.         //If cloud is off Screen, Destroy it.
    30.         if(transform.position.x - buffer > camWidth) {
    31.             Destroy(gameObject);
    32.         }
    33.     }
    34. }
     
  2. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    58
    If (score % 10 == 0)
    Spawn()
     
  3. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    58
    I don't see you are calling the spawnpoint method anywhere (if i havent missed something as I am readong this on mobile).
    Just call it in update method for example.
     
    Jojoba007 likes this.
  4. Jojoba007

    Jojoba007

    Joined:
    Feb 11, 2017
    Posts:
    148
    The score was working allready, but had to put it in Update() {SpwanCoin();}. Stupid me.