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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.VFX; public class Rocks : MonoBehaviour { // death support //Timer deathTimer; void Start() { /// <summary> /// Use this for initialization /// </summary> // apply impulse force to get the object moving const float MinImpulseForce = 2f; const float MaxImpulseForce = 3f; float angle = Random.Range(0, 2 * Mathf.PI); Vector2 direction = new Vector2( Mathf.Cos(angle), Mathf.Sin(angle)); float magnitude = Random.Range(MinImpulseForce, MaxImpulseForce); GetComponent<Rigidbody2D>().AddForce( direction * magnitude, ForceMode2D.Impulse); ///<summary> ///Use this for initialization ///</summary> // create and start timer //deathTimer = deathTimer = gameObject.AddComponent<Timer>(); //deathTimer.Duration = greenrocklifeseconds; //deathTimer.Duration = magentarocklifeseconds; //deathTimer.Duration = whiterocklifeseconds; //deathTimer.Run(); } // Update is called once per frame void Update() { //if (deathTimer != null && deathTimer.Finished) //{ // Destroy(gameObject); //} } ///<summary> ///Self-destruct when sprites leave scene ///</summary> void OnBecameInvisible() { Destroy(gameObject); var newObject = Instantiate(gameObject, new Vector3(), new Quaternion()); newObject.SetActive(true); } public static void SpawnRock() { } public static void newObject() { SpawnRock(); } } ------------------------------------------------------------------------------------------------------------------------------------------------ RockSpawner.cs code Code (CSharp): using System.Collections; using System.Collections.Generic; using System.Threading; using UnityEngine; /// <summary> /// A rock spawner /// </summary> public class RockSpawner : MonoBehaviour { // needed for spawning [SerializeField] GameObject prefabRocks; // saved for efficiency [SerializeField] Sprite greenrock; [SerializeField] Sprite magentarock; [SerializeField] Sprite whiterock; // spawn control const float MinSpawnDelay = 1; const float MaxSpawnDelay = 2; Timer spawnTimer; // spawn location support const int SpawnBorderSize = 100; int minSpawnX; int maxSpawnX; int minSpawnY; int maxSpawnY; /// <summary> /// Use this for initialization /// </summary> void start() { // save spawn boundaries for efficiency minSpawnX = SpawnBorderSize; maxSpawnX = Screen.width - SpawnBorderSize; minSpawnY = SpawnBorderSize; maxSpawnY = Screen.height - SpawnBorderSize; // create and save timer spawnTimer = gameObject.AddComponent<Timer>(); spawnTimer.Duration = Random.Range(MinSpawnDelay, MaxSpawnDelay); spawnTimer.Run(); } /// <summary> /// Update is called once per frame /// </summary> void update() { // check for time to spawn a new random rock if (spawnTimer.Finished) { SpawnRock(); } // change spawn timer duration and restart spawnTimer.Duration = Random.Range(MinSpawnDelay, MaxSpawnDelay); spawnTimer.Run(); } /// <summary> /// spawns a new rock at a random location /// </summary> public void SpawnRock() { // generate random location and create a new rock Vector3 location = new Vector3(Random.Range(minSpawnX, maxSpawnX), Random.Range(minSpawnY, maxSpawnY), -Camera.main.transform.position.z); Vector3 worldLocation = Camera.main.ScreenToWorldPoint(location); GameObject Rock = Instantiate(prefabRocks) as GameObject; Rock.transform.position = worldLocation; // set random sprite for new rock SpriteRenderer spriteRenderer = prefabRocks.GetComponent<SpriteRenderer>(); int SpriteNumber = Random.Range(0, 3); if (SpriteNumber == 0) { spriteRenderer.sprite = greenrock; } else if (SpriteNumber == 1) spriteRenderer.sprite = magentarock; else { spriteRenderer.sprite = whiterock; } } }
Please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/ How to report problems productively in the Unity3D forums: http://plbm.com/?p=220
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?