Search Unity

Question Spawning rocks

Discussion in 'Scripting' started by YooOwnIt, Jul 3, 2020.

  1. YooOwnIt

    YooOwnIt

    Joined:
    Jul 3, 2020
    Posts:
    23
    Hello,
    I'm new to Unity and C#, so I might be doing some things incorrectly.

    Basically for my project I have to (1) make three rocks move at a random velocity and go at a random direction. (2) A rock gets destroyed after it leaves the scene (for me, it appears to be the camera). After a rock gets destroyed, (3) a new one spawns in the scene and repeats step (1) so that there are never less than three rocks at any time.

    I am able to move the first three rocks but the new rocks that spawn do not move. Is there a way to fix this?

    I have two C# scripts: "Rocks.cs" and "RockSpawner.cs", and both are attached to the three prefab rocks/sprites.

    Thanks in advance.
    Rocks.cs code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.VFX;
    5. public class Rocks : MonoBehaviour
    6. {
    7.     // death support
    8.  
    9.     //Timer deathTimer;
    10.     void Start()
    11.     {
    12.         /// <summary>
    13.         /// Use this for initialization
    14.         /// </summary>
    15.         // apply impulse force to get the object moving
    16.         const float MinImpulseForce = 2f;
    17.         const float MaxImpulseForce = 3f;
    18.         float angle = Random.Range(0, 2 * Mathf.PI);
    19.         Vector2 direction = new Vector2(
    20.             Mathf.Cos(angle), Mathf.Sin(angle));
    21.         float magnitude = Random.Range(MinImpulseForce, MaxImpulseForce);
    22.         GetComponent<Rigidbody2D>().AddForce(
    23.             direction * magnitude,
    24.             ForceMode2D.Impulse);
    25.         ///<summary>
    26.         ///Use this for initialization
    27.         ///</summary>
    28.         // create and start timer
    29.         //deathTimer = deathTimer = gameObject.AddComponent<Timer>();
    30.         //deathTimer.Duration = greenrocklifeseconds;
    31.         //deathTimer.Duration = magentarocklifeseconds;
    32.         //deathTimer.Duration = whiterocklifeseconds;
    33.         //deathTimer.Run();
    34.     }
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         //if (deathTimer != null && deathTimer.Finished)
    39.         //{
    40.         //    Destroy(gameObject);
    41.         //}
    42.     }
    43.     ///<summary>
    44.     ///Self-destruct when sprites leave scene
    45.     ///</summary>
    46.     void OnBecameInvisible()
    47.     {
    48.         Destroy(gameObject);
    49.      
    50.         var newObject = Instantiate(gameObject, new Vector3(), new Quaternion());
    51.      
    52.             newObject.SetActive(true);
    53.         }
    54.     public static void SpawnRock()
    55.     {
    56.     }
    57.     public static void newObject()
    58.     {
    59.         SpawnRock();
    60.     }
    61.     }
    ------------------------------------------------------------------------------------------------------------------------------------------------
    RockSpawner.cs code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5. /// <summary>
    6. /// A rock spawner
    7. /// </summary>
    8. public class RockSpawner : MonoBehaviour
    9. {
    10.     // needed for spawning
    11.     [SerializeField]
    12.     GameObject prefabRocks;
    13.     // saved for efficiency
    14.     [SerializeField]
    15.     Sprite greenrock;
    16.     [SerializeField]
    17.     Sprite magentarock;
    18.     [SerializeField]
    19.     Sprite whiterock;
    20.     // spawn control
    21.     const float MinSpawnDelay = 1;
    22.     const float MaxSpawnDelay = 2;
    23.     Timer spawnTimer;
    24.     // spawn location support
    25.     const int SpawnBorderSize = 100;
    26.     int minSpawnX;
    27.     int maxSpawnX;
    28.     int minSpawnY;
    29.     int maxSpawnY;
    30.     /// <summary>
    31.     /// Use this for initialization
    32.     /// </summary>
    33.     void start()
    34.     {
    35.         // save spawn boundaries for efficiency
    36.         minSpawnX = SpawnBorderSize;
    37.         maxSpawnX = Screen.width - SpawnBorderSize;
    38.         minSpawnY = SpawnBorderSize;
    39.         maxSpawnY = Screen.height - SpawnBorderSize;
    40.         // create and save timer
    41.         spawnTimer = gameObject.AddComponent<Timer>();
    42.         spawnTimer.Duration = Random.Range(MinSpawnDelay, MaxSpawnDelay);
    43.         spawnTimer.Run();
    44.     }
    45.     /// <summary>
    46.     /// Update is called once per frame
    47.     /// </summary>
    48.     void update()
    49.     {
    50.         // check for time to spawn a new random rock
    51.         if (spawnTimer.Finished)
    52.         {
    53.             SpawnRock();
    54.         }
    55.         // change spawn timer duration and restart
    56.         spawnTimer.Duration = Random.Range(MinSpawnDelay, MaxSpawnDelay);
    57.         spawnTimer.Run();
    58.     }
    59.     /// <summary>
    60.     /// spawns a new rock at a random location
    61.     /// </summary>
    62.     public void SpawnRock()
    63.     {
    64.         // generate random location and create a new rock
    65.         Vector3 location = new Vector3(Random.Range(minSpawnX, maxSpawnX),
    66.         Random.Range(minSpawnY, maxSpawnY),
    67.         -Camera.main.transform.position.z);
    68.         Vector3 worldLocation = Camera.main.ScreenToWorldPoint(location);
    69.         GameObject Rock = Instantiate(prefabRocks) as GameObject;
    70.         Rock.transform.position = worldLocation;
    71.         // set random sprite for new rock
    72.         SpriteRenderer spriteRenderer = prefabRocks.GetComponent<SpriteRenderer>();
    73.         int SpriteNumber = Random.Range(0, 3);
    74.         if (SpriteNumber == 0)
    75.         {
    76.             spriteRenderer.sprite = greenrock;
    77.         }
    78.         else if (SpriteNumber == 1)
    79.             spriteRenderer.sprite = magentarock;
    80.         else
    81.         {
    82.             spriteRenderer.sprite = whiterock;
    83.         }
    84.     }
    85. }
     
    Last edited: Jul 3, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
  3. YooOwnIt

    YooOwnIt

    Joined:
    Jul 3, 2020
    Posts:
    23
    Fixed.

    sorry if it wasn't specific enough. I am trying to apply Rocks.cs lines #16-24 to get the newly spawned rocks to move again, and for the process to repeat indefinitely. How can I call those lines to get the cloned rocks to move?