Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

C# Random Object Spawner Help

Discussion in 'Scripting' started by cyanjamgames_thomas, Dec 20, 2018.

  1. cyanjamgames_thomas

    cyanjamgames_thomas

    Joined:
    Oct 15, 2018
    Posts:
    18
    Hi,

    If anyone knows what is wrong with the code below then please tell me because I'm so confused. It's basically supposed to randomly spawn one of three apple variants (in one of three specific X co-ordinates) that move down and eventually off-screen.

    What it's actually doing is spawning the apple at 0, 0, 2 (not mentioned anywhere in my code) and not moving the apple downwards.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class appleSpawn : MonoBehaviour {
    7.  
    8.     public int appleID;
    9.     public int colID;
    10.  
    11.     public System.Random generator = new System.Random();
    12.  
    13.     public GameObject redApple;
    14.     public GameObject goldApple;
    15.     public GameObject poisonApple;
    16.  
    17.     public GameObject playerSprite;
    18.  
    19.     public int spawnLeastWait = 3000;
    20.     public int spawnMostWait = 8000;
    21.  
    22.     public Vector3 spawnPOS;
    23.  
    24.     void Start()
    25.     {
    26.         StartCoroutine(Spawner());
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         if (appleID == 0)
    32.         {
    33.             redApple.transform.Translate(0, -1, 0);
    34.             Debug.Log("Red");
    35.             if (redApple.transform.position.y < -9409)
    36.             {
    37.                 Destroy(redApple);
    38.             }
    39.             else if (playerSprite.transform.position == redApple.transform.position)
    40.             {
    41.                 Player.Player.RedApple(Player.Player.points, Player.Player.speed);
    42.             }
    43.         }
    44.  
    45.         if (appleID == 1)
    46.         {
    47.             goldApple.transform.Translate(0, -1, 0);
    48.             if (goldApple.transform.position.y < -9409)
    49.             {
    50.                 Destroy(goldApple);
    51.             }
    52.             else if (playerSprite.transform.position == goldApple.transform.position)
    53.             {
    54.                 Player.Player.GoldenApple(Player.Player.points, Player.Player.speed, Player.Player.health);
    55.             }
    56.         }
    57.  
    58.         if (appleID == 2)
    59.         {
    60.             poisonApple.transform.Translate(0, -1, 0);
    61.             if (poisonApple.transform.position.y < -9409)
    62.             {
    63.                 Destroy(poisonApple);
    64.             }
    65.             else if (playerSprite.transform.position == poisonApple.transform.position)
    66.             {
    67.                 Player.Player.PoisonApple(Player.Player.points, Player.Player.speed, Player.Player.health);
    68.             }
    69.         }
    70.  
    71.     }
    72.  
    73.     IEnumerator Spawner() {
    74.         appleID = generator.Next(0, 2);
    75.         colID = generator.Next(0, 2);
    76.  
    77.         if (colID == 0)
    78.         {
    79.             spawnPOS = new Vector3(playerMovement.col1.x, 0, 2);
    80.         }
    81.  
    82.         else if (colID == 1)
    83.         {
    84.             spawnPOS = new Vector3(playerMovement.col2.x, 0, 2);
    85.         }
    86.  
    87.         else if (colID == 2)
    88.         {
    89.             spawnPOS = new Vector3(playerMovement.col3.x, 0, 2);
    90.         }
    91.  
    92.         yield return new WaitForSeconds(2);
    93.  
    94.         if (appleID == 0)
    95.         {
    96.             Instantiate(redApple, spawnPOS, Quaternion.identity);
    97.         }
    98.  
    99.         else if (appleID == 1)
    100.         {
    101.             Instantiate(goldApple, spawnPOS, Quaternion.identity);
    102.         }
    103.  
    104.         else if (appleID == 2)
    105.         {
    106.             Instantiate(poisonApple, spawnPOS, Quaternion.identity);
    107.         }
    108.  
    109.     }
    110. }
    Thanks.
     
  2. cyanjamgames_thomas

    cyanjamgames_thomas

    Joined:
    Oct 15, 2018
    Posts:
    18
    Oh and the ints spawnLeastWait and spawnMostWait are currently unused, at the moment I just want one every two seconds but that isn't working either.
     
  3. cyanjamgames_thomas

    cyanjamgames_thomas

    Joined:
    Oct 15, 2018
    Posts:
    18
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Line 76 - 90. Your col1, col2, col3 is probably 0. Hence why 0,0,2 position.

    I don't see the code that should move the apple afterwards, maybe that's why it doesn't move it at all?
    Do you have a script attached to the apple that is supposed to move it?

    Note that you cannot move it like you've wrote it in Update. You're checking against prefab, and your actual Instantiate result isn't stored anywhere.

    If you want to move your instantiated apples, store them @ E.g. List.
    Code (CSharp):
    1. private List<GameObject> _redApples = new List<GameObject>();
    2.  
    3. // Then @ Instantiate
    4. GameObject go = Instantiate(...);
    5.  
    6. _redApples.Add(go);
    7.  
    8. // Then in update -> iterate over the list and move the apples.
    But it's simpler to attach an Apple script to the prefab that moves the apple instead.
     
    cyanjamgames_thomas likes this.
  5. cyanjamgames_thomas

    cyanjamgames_thomas

    Joined:
    Oct 15, 2018
    Posts:
    18
    Hi

    Thanks, I managed to fix it in the end. Col1, Col2 and Col3 all have seperate x values (none 0) so I’m confused as to why it always spawns at x 0 though. Either way the Apple now moves downwards but I’m having another problem where 2D collision basically isn’t working.

    I posted my code on this thread, and I have added a RigidBody but that hasn’t solved the problem: https://forum.unity.com/threads/istouching-not-working.399162/

    Thanks
     
  6. cyanjamgames_thomas

    cyanjamgames_thomas

    Joined:
    Oct 15, 2018
    Posts:
    18
    OK, I now have fully working code that spawns one apple on-screen at a time, with a 2 second delay between each spawn. How can I increase the frequency of the spawns (i.e. multiple on screen at once) without spawning one every frame? Thanks.

    Code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class appleSpawn : MonoBehaviour {
    7.  
    8.     public int appleID;
    9.     public int colID;
    10.  
    11.     public int currentApple;
    12.  
    13.     public System.Random generator = new System.Random();
    14.  
    15.     public GameObject redApple;
    16.     public GameObject goldApple;
    17.     public GameObject poisonApple;
    18.     public GameObject coin;
    19.  
    20.     public List<GameObject> apples = new List<GameObject>();
    21.  
    22.     public GameObject playerSprite;
    23.  
    24.     public Collider2D playerCollision;
    25.     public Collider2D appleCollision;
    26.  
    27.     public Vector3 spawnPOS;
    28.  
    29.     void Start()
    30.     {
    31.         currentApple = 0;
    32.         StartCoroutine(Spawner());
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         if (Player.Player.health <= 0)
    38.         {
    39.             Destroy(apples[currentApple]);
    40.         }
    41.  
    42.         if (colID == 0)
    43.         {
    44.             spawnPOS = new Vector3(-435, 2400, 2);
    45.         }
    46.  
    47.         else if (colID == 1)
    48.         {
    49.             spawnPOS = new Vector3(16, 2400, 2);
    50.         }
    51.  
    52.         else if (colID == 2)
    53.         {
    54.             spawnPOS = new Vector3(401, 2400, 2);
    55.         }
    56.  
    57.         apples[currentApple].transform.Translate(new Vector3(0, -1, 0) * Player.Player.speed * Time.deltaTime);
    58.  
    59.         if (apples[currentApple].transform.position.y < -4000) {
    60.             Destroy(apples[currentApple]);
    61.             currentApple = currentApple + 1;
    62.             StartCoroutine(Spawner());
    63.         }
    64.  
    65.         else if (appleCollision.IsTouching(playerCollision))
    66.         {
    67.             Destroy(apples[currentApple]);
    68.  
    69.             if (appleID == 0)
    70.             {
    71.                 Player.Player.RedApple(Player.Player.points, Player.Player.speed);
    72.             }
    73.  
    74.             else if(appleID == 1)
    75.             {
    76.                 Player.Player.GoldenApple(Player.Player.points, Player.Player.speed, Player.Player.health);
    77.             }
    78.  
    79.             else if(appleID == 2)
    80.             {
    81.                 Player.Player.PoisonApple(Player.Player.points, Player.Player.speed, Player.Player.health);
    82.             }
    83.  
    84.             else if (appleID == 3)
    85.             {
    86.                 Player.Player.coins = Player.Player.coins + 50;
    87.                 Debug.Log(Player.Player.coins);
    88.             }
    89.  
    90.             currentApple = currentApple + 1;
    91.             StartCoroutine(Spawner());
    92.         }
    93.  
    94.     }
    95.  
    96.     IEnumerator Spawner() {
    97.         yield return new WaitForSeconds(3);
    98.  
    99.         appleID = generator.Next(0, 4);
    100.         colID = generator.Next(0, 3);
    101.  
    102.         yield return new WaitForSeconds(2);
    103.  
    104.         if (appleID == 0)
    105.         {
    106.             apples.Add(Instantiate(redApple, spawnPOS, Quaternion.identity));
    107.         }
    108.  
    109.         if (appleID == 1)
    110.         {
    111.             apples.Add(Instantiate(goldApple, spawnPOS, Quaternion.identity));
    112.         }
    113.  
    114.         if (appleID == 2)
    115.         {
    116.             apples.Add(Instantiate(poisonApple, spawnPOS, Quaternion.identity));
    117.            
    118.         }
    119.  
    120.         if (appleID == 3)
    121.         {
    122.             apples.Add(Instantiate(coin, spawnPOS, Quaternion.identity));
    123.         }
    124.  
    125.         appleCollision = apples[currentApple].AddComponent<BoxCollider2D>();
    126.     }
    127. }
    128.