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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

C# Random sprite spawn script (help)

Discussion in 'Scripting' started by Venatal, Nov 12, 2015.

  1. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    How would I write code in C# that would randomly spawn 1 of 4 sprites in a set position that spawns every second and also the sprite has a random chance of traveling along the X axis (left or right) or the Y axis (up or down)
    I'm new to coding so can you explain it in layman terms, thanks!
     
  2. rchmiel

    rchmiel

    Joined:
    Apr 5, 2015
    Posts:
    49
    For random volume, direction move or something else use Random.Range(min, max). To turn on function after second use yield.
    I made this on knee so maybe something doesn't work:
    Code (csharp):
    1. public Transform someObject; // add object in Inspector
    2. public Transform spawnArea; // add spawn point (empty game object) and set rotation;
    3. Transform spawnDirection = Vector3.zero;
    4. Transform direction = Vector3.zero;
    5.  
    6. void Start()
    7. {
    8.    nextSpawn(); // start spawn loop
    9. }
    10.  
    11. IEnumerator nextSpawn() // wait one second and run next function
    12. {
    13.    yield return new WaitForSeconds(1); // one second
    14.    Spawn();
    15. }
    16.  
    17. void Spawn() // put objects on scene
    18. {
    19.    float howmuch = Random.Range(1,4);
    20.    for(int i = 0; i < howmany; i++) // for all objects do the same
    21.    {
    22.       // if 0 don't rotate, if 1 turn left, if 2 turn right (remember rotation of your spawnArea)
    23.       int dir = Random.Range(0,1);
    24.       direction = spawnArea.rotation.eulerAngles.y; // actual spawnArea rotation
    25.       if(dir == 1) { direction += 90; }
    26.       else if(dir == 2) { direction -= 90; }
    27.      
    28.       spawnDirection.rotation = Quaternion.Euler(0, direction, 0);    
    29.  
    30.      // create new object on scene
    31.      Instantiate(someObject, spawnArea.position, spawnDirection.rotation);
    32.    }
    33.  
    34.    //  at the end, start again nextSpawn
    35.    nextSpawn(); // loop
    36. }
     
    Last edited: Nov 12, 2015
  3. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Its showing me that there are compiler errors.
    Assets/Scripts/SpawnControl.cs(21,8): error CS1525: Unexpected symbol `Spawn'
    Assets/Scripts/SpawnControl.cs(35,16): error CS1525: Unexpected symbol `else'
     
  4. rchmiel

    rchmiel

    Joined:
    Apr 5, 2015
    Posts:
    49
    I corrected first code - look up.
    in 21 put the end of line ;
    in 34 also put ;
     
  5. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Is there some way to contact you because I'm still getting errors?
     
  6. rchmiel

    rchmiel

    Joined:
    Apr 5, 2015
    Posts:
    49
    Switch to Inbox and click on "conversation".