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

[Solved] Instantiate a GameObject randomly between 2 rotation values

Discussion in 'Scripting' started by krousty_bat, Mar 7, 2016.

  1. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Hello

    I'm trying to spawn GameObjects (walls) on their original rotation (0) or randomly turned 180 too. this is what I tried:

    Code (CSharp):
    1. {
    2. // get the current spawner position
    3. Vector3 startPos = transform.position;
    4.  
    5. // this quaternion corresponds to no rotation
    6. Quaternion spawnRotation[0]=Quaternion.identity;
    7. // 180 turned value
    8. Quaternion spawnRotation[1]=Quaternion.Euler(0,0,180);
    9.  
    10. // what I thought would work to Instantiate my wall at startPos with a rotation random between spawnRotation 0 and 1
    11. Instantiate(wall, startPos, spawnRotation[Random.Range(0,2)]);                
    12. }
    But I have these Red Alerts :)

    and
    Starting to learn C#, Can someone tell me what i'm doing technically wrong here please ?

    Thx a lot !

    edit (adding ; at ligne 8)
     
    Last edited: Mar 7, 2016
  2. klisman

    klisman

    Joined:
    Jun 15, 2015
    Posts:
    31
    Missing ; at line 8
    Code (CSharp):
    1. Quaternion spawnRotation[1]=Quaternion.Euler(0,0,180);
     
  3. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Yeah, sorry, I realized that
    but all the messages are the same even with it ;)
     
  4. klisman

    klisman

    Joined:
    Jun 15, 2015
    Posts:
    31
    Code (CSharp):
    1. Random.Range(0,1)
    For two options.
    Code (CSharp):
    1. Random.Range(0,2)
    Provides 3 options, in your case the third is empty.

    I can't see all your code but you need to start your array somewhere.
    Code (CSharp):
    1. public class sdf : MonoBehaviour {
    2.     Quaternion[] spawnRotation;
    3.     // Use this for initialization
    4.     void Start () {
    5.         spawnRotation = new Quaternion[2];
    6.     }
    7. }
    8.  
     
  5. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    How are you declaring the spawnRotation array?
     
  6. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Thx
    so I was missing I guess an Array limits... here is my new code
    Code (CSharp):
    1. Quaternion spawnRotation = new Quaternion[2];
    2.  
    3.     IEnumerator SpawnSetup ()
    4.                 {
    5.                     Vector3 startPos = transform.position;
    6.                     spawnRotation[0] = Quaternion.identity;
    7.                     spawnRotation[1] = Quaternion.Euler(0, 0, 180);
    8.                     Instantiate(wall, startPos, spawnRotation[Random.Range(0,1)]);
    9.  
    10.                     yield return new WaitForSeconds(spawnRate);
    11.                 }
    but now I have this for ligne 1 :)

     
  7. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    Code (CSharp):
    1. Quaternion[] spawnRotation = new Quaternion[2];
    2. spawnRotation[0] = Quaternion.identity;
    3. spawnRotation[1] = Quaternion.Euler(0,0,180);
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    ShokeR0 is right. Alternate, simplified syntax:
    Code (csharp):
    1. Quaternion[] spawnRotation = new Quaternion[]{Quaternion.identity, Quaternion.Euler(0f, 0f, 180f)};
    2. Instantiate(wall, startPos, spawnRotation[Random.Range(0, spawnRotation.Length)]);
    If you, for example, decide to add more rotations later, you can just stick them in that list at the end of the first line, and it'll Just Work(TM).
     
    krousty_bat likes this.
  9. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Thx A lot...
    I think I had the idea, but did few rookie mistakes!

    Thx again !