Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with coding for special simulation

Discussion in 'Scripting' started by RayneAvalon, Oct 10, 2012.

  1. RayneAvalon

    RayneAvalon

    Joined:
    Oct 10, 2012
    Posts:
    5
    Hello everyone, I am completely new to Unity and have been given a project to do for school that is very important. I need help getting some very specific coding to work so that i can move forward with the project. I need to be able to create a group of menus that allows the player to click a button and spawn a maya object in the game that they can then use to build a house. i also need to know how to write code so that the player can then move any such spawned object as if they were in the editor. after they have the house built they then need to be able to set it on fire. so can anyone help me fix the code below so that my buttons will spawn the necessary objects?
    Code (csharp):
    1.  
    2. var test1:GameObject;
    3.  
    4. function OnGUI () {
    5.     // Make Flamable Materials Menu Box
    6.     GUI.Box (Rect (10,10,120,300), "Flamable Materials");
    7.  
    8.     // Make the first button. If it is pressed, the Truss asset will appear on screen.
    9.     if (GUI.Button (Rect (20,40,80,20), "Truss")) {
    10.         Instantiate(test1,Vector3(0,0,0), Quaternion.identity);
    11.     }
    12.  
    13.     // Make the second button. If it is pressed, a 2x4x8 Stud asset will appear on screen
    14.     if (GUI.Button (Rect (20,70,85,20), "Stud: 2x4x8")) {
    15.         print("This will spawn a 2x4x8 stud at the origin");
    16.     }
    17.    
    18.     // Make the third button.If it is pressed, a 2x4x3 Stud asset will appear on screen
    19.     if (GUI.Button (Rect (20,100,85,20), "Stud: 2x4x3")) {
    20.         print("This will spawn a 2x4x3 stud at the origin");
    21.     }
    22.    
    23.     // Make the fourth button.If it is pressed, a 2x4x1 Stud asset will appear on screen
    24.     if (GUI.Button (Rect (20,130,85,20), "Stud: 2x4x1")) {
    25.         print("This will spawn a 2x4x1 stud at the origin");
    26.         // Make the second button.
    27.  
    28.     }
    29.     // Make the fifth button.If it is pressed, a Subfloor asset will appear on screen
    30.     if (GUI.Button (Rect (20,160,85,20), "Subfloor")) {
    31.         print("This will spawn a Subfloor pannel at the origin");
    32.     }
    33.    
    34.         // Make the fifth button.If it is pressed, a Carpet asset will appear on screen
    35.     if (GUI.Button (Rect (20,190,85,20), "Carpet")) {
    36.         print("This will spawn a piece of Carpet at the origin");
    37.     }
    38.    
    39.         // Make the sixth button.If it is pressed, a Drywall asset will appear on screen
    40.     if (GUI.Button (Rect (20,220,85,20), "Drywall: 4x8")) {
    41.         print("This will spawn a 4x8 Drywall pannel at the origin");
    42.     }
    43.             // Make the seventh button.If it is pressed, a Drywall asset will appear on screen
    44.     if (GUI.Button (Rect (20,250,85,20), "Drywall: 3x8")) {
    45.         print("This will spawn a 3x8 Drywall pannel at the origin");
    46.     }
    47.             // Make the eighth button.If it is pressed, a Drywall asset will appear on screen
    48.     if (GUI.Button (Rect (20,280,85,20), "Drywall: 1x1")) {
    49.         print("This will spawn a 1x1 Drywall pannel at the origin");
    50.     }
    51.    
    52.     // Make Non-Flamable Materials Menu Box
    53.     GUI.Box (Rect (140,10,150,180), "Non-Flamable Materials");
    54.  
    55.     // Make the First button.If it is pressed, a Door asset will appear on screen
    56.     if (GUI.Button (Rect (170,40,80,20), "Door")) {
    57.         print("This will spawn a Door at the origin");
    58.     }
    59.  
    60.     // Make the second button.If it is pressed, a Sliding glass door asset will appear on screen
    61.     if (GUI.Button (Rect (160,70,120,20), "Sliding glass door")) {
    62.         print("This will spawn a Sliding Glass Door at the origin");
    63.     }
    64.    
    65.     // Make the third button.If it is pressed, a Window asset will appear on screen
    66.     if (GUI.Button (Rect (170,100,85,20), "Window")) {
    67.         print("This will spawn a Window at the origin");
    68.     }
    69.    
    70.     // Make the Fourth button.If it is pressed, a Foundation asset will appear on screen
    71.     if (GUI.Button (Rect (170,130,85,20), "Foundation")) {
    72.         print("This will spawn a Foundation at the origin");
    73.  
    74.     }
    75.     // Make the Fourth button.If it is pressed, a Tile Floor asset will appear on screen
    76.         if (GUI.Button (Rect (170,160,85,20), "Tile Floor")) {
    77.         print("This will spawn a Tile Floor Pannel at the origin");
    78.     }
    79.    
    80.    
    81. }
    the code as of now is designed to only spawn a cube that im using as a test to see if the code works. every time i click the button it is assigned to i get an error message saying "UnassignedReferenceException: The variable test1 of 'MenuButton' has not been assigned." however in the inspector i have it assigned to a TestCube object in my assets folder. can anyone tell me what im doing wrong or why im getting this error?
     
  2. CrazySi

    CrazySi

    Joined:
    Jun 23, 2011
    Posts:
    538
    What do you think that means?
     
  3. RayneAvalon

    RayneAvalon

    Joined:
    Oct 10, 2012
    Posts:
    5
    im not sure, when i look in my inspector window it shows the variable test1 is assigned the target of TestCube. like i said man im completely new to this. i don't understand most of what im doing. i get the very basics and thats it.
     
  4. RayneAvalon

    RayneAvalon

    Joined:
    Oct 10, 2012
    Posts:
    5
    so is there anyone out there that can help me? or am i completely on my own here?
     
  5. CrazySi

    CrazySi

    Joined:
    Jun 23, 2011
    Posts:
    538
    I can't help you much because I don't use that strange language dialect. Sorry.
    If it was in C# I could give an answer.
     
  6. Brian-Sinasac

    Brian-Sinasac

    Joined:
    Sep 8, 2012
    Posts:
    97
    Let me make sure I understand what you've done. The above code has been attached to the MainCamera in your scene. You have created a prefab object that is simply a cube. You then selected the MainCamera and in the inspector you dragged the prefab object on to the test1 variable?
     
  7. RayneAvalon

    RayneAvalon

    Joined:
    Oct 10, 2012
    Posts:
    5
    sorry i let this go for so long. a buddy of mine at school showed me what i was doing wrong. apparently i wasn't setting up my target variable right. i have solved the problem now.