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. Dismiss Notice

My random dungeon generator script wont work. (C#)

Discussion in 'Scripting' started by AndroidBebop, Feb 14, 2015.

  1. AndroidBebop

    AndroidBebop

    Joined:
    Dec 19, 2013
    Posts:
    7
    I am trying to randomize a room in my dungeon and then instantiate it at a set position.
    Thank you for your time.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Dungeon_Generator : MonoBehaviour {
    5.  
    6.     public GameObject[] Corridor1;
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.  
    16.     }
    17.     void OnGui()
    18.     {
    19.         if(GUI.Button(new Rect(40,50,100,40),"Start"))
    20.         {
    21.             //corridor
    22.        
    23.             Random.Range(0,Corridor1);
    24.             GameObject Corri = Instantiate (Corridor1,transform.position, transform.rotation);
    25.             Instantiate (Corri, new Vector3(2,2,2), transform.rotation);
    26.             Debug.Log("Clicked");
    27.         }
    28.     }
    29. }
    30.  
     
    Last edited: Feb 14, 2015
  2. Yemachu

    Yemachu

    Joined:
    Feb 1, 2015
    Posts:
    13
    The Console in Unity should give some hints on why it is not working.

    First, you are trying to treat an array of objects as a floating point number. This is solved by using the "Length" property of an array. Though there is currently no point in using that function, as you do nothing with the result; but more on that later.

    Then you pass that same array of objects to a function which only takes one element of it. Here you need to select a single element from it. I assume you wanted a random element, so you need to save the result of the previous function. Then retrieve that element from the array (using square brackets). Also you need to add a type cast in order to store it as a GameObject.

    With that out of the way, you instantiate the object once again... for no appearant reason. This is not necessarily wrong, but I have a feeling that you only need 1. In that case you could pass that Vector3 as an argument to the function on the previous line.

    Lastly your function is called "OnGui". Though the compiler won't complain, it will most likely do nothing; Unity searches for the function "OnGUI" and that is case-sensitive. So it will most likely not find it. (GUI stands for Graphical User Interface).

    Without futher ado, here is a script which should do something:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Dungeon_Generator : MonoBehaviour
    5. {
    6.     //Why is called Corridor1, and not corridorParts or something alike?
    7.     public GameObject[] Corridor1;
    8.  
    9.     void OnGUI()
    10.     {
    11.         if(GUI.Button(new Rect(40,50,100,40),"Start"))
    12.         {
    13.             int i = Random.Range(0,Corridor1.Length);
    14.  
    15.             //If you arent going to use the reference in this function, you could better remove the assignment.
    16.             //NOTE: I combined the result from the next 2 lines from the original.
    17.             GameObject Corri = (GameObject)Instantiate (Corridor1[i], new Vector3(2,2,2), transform.rotation);
    18.  
    19.             //Next line is seems redundant; change it as you see fit.
    20.             //Instantiate (Corri, new Vector3(2,2,2), transform.rotation);
    21.             Debug.Log("Clicked");
    22.         }
    23.     }
    24. }
     
    AndroidBebop likes this.
  3. AndroidBebop

    AndroidBebop

    Joined:
    Dec 19, 2013
    Posts:
    7
    I found a solution
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Dungeon_Generator : MonoBehaviour {
    5.  
    6.     public GameObject[] Corridor1;
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.  
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17.     void OnGUI()
    18.     {
    19.         if(GUI.Button(new Rect(40,50,100,40),"Start"))
    20.         {
    21.  
    22.             Instantiate (Corridor1 [Random.Range(0,Corridor1.Length)],transform.position = new Vector3(1,1,1), transform.rotation);
    23.         }
    24.     }
    25. }
    26.  
     
  4. AndroidBebop

    AndroidBebop

    Joined:
    Dec 19, 2013
    Posts:
    7
    Thank you. :), I just saw your post after I posted.
     
    Last edited: Feb 14, 2015