Search Unity

ERROR (Please Help Me Fix)

Discussion in 'Editor & General Support' started by nathan3228, Sep 15, 2011.

  1. nathan3228

    nathan3228

    Joined:
    Jun 28, 2011
    Posts:
    24
    :( I am normaly good at unity3d but this has got me.

    Error: BCE0034: Expressions in statements must only be executed for their side-effects.

    My Code:
    Code (csharp):
    1.  
    2.    function OnGUI() {
    3.  
    4.   GUI.skin = Skin[cont%Skin.Length];
    5.  
    6.    if(Skin.Length == 0){
    7.  
    8.     Debug.LogError("Assign at least 1 sking on the array");
    9.  
    10.     return;
    11.  
    12.    }
    13.  
    14.  
    15.  
    16.    if (GUI.Button(Rect(600,300,300,60),"Builder")) {
    17.  
    18.    
    19.  
    20.     var spawnpoints : GameObject[] = GameObject.FindGameObjectsWithTag ("Spawnpoint");
    21.  
    22.     Debug.Log("spawns: "+spawnpoints.length);
    23.  
    24.    
    25.  
    26.     var spawnpoint : Transform = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
    27.  
    28.     var newTrans : Transform = Network.Instantiate(builder,spawnpoint.position, spawnpoint.rotation, 0) as Transform;
    29.  
    30.     chatScript.addGameChatMessage(name+" is a Builder");
    31.  
    32.     Destroy (this);
    33.  
    34.  
    35.  
    36.    }
    37.  
    38.    
    39.  
    40.    if (GUI.Button(Rect(600,200,300,60),"Cook")) {
    41.  
    42.    
    43.  
    44.     spawnpoints;
    45.  
    46.  
    47.  
    48.        
    49.     spawnpoint;
    50.  
    51.     newTrans;
    52.  
    53.     chatScript.addGameChatMessage(name+" is a Cook");
    54.  
    55.     Destroy (this);
    56.  
    57.  
    58.  
    59.    }
    60.  
    61.    
    62.  
    63.    if (GUI.Button(Rect(600,100,300,60),"Hunter")) {
    64.  
    65.    
    66.  
    67.     spawnpoints;
    68.  
    69.    
    70.  
    71.    
    72.     spawnpoint;
    73.  
    74.     newTrans;
    75.  
    76.     chatScript.addGameChatMessage(name+" is a Hunter");
    77.  
    78.     Destroy (this);
    79.  
    80.  
    81.  
    82.    }
    83.  
    84. }
    85.  
    86.  
    87.  
    Please help me fix this error.
     
  2. deram_scholzara

    deram_scholzara

    Joined:
    Aug 26, 2005
    Posts:
    1,043
    well... there are several spots that are making this come up, but essentially your problem lies in lines like these:
    Code (csharp):
    1.  spawnpoints;
    2.  
    3.     spawnpoint;
    4.  
    5.     newTrans;
    spawnpoints, spawnpoint, and newTrans are all variables. By writing "spawnpoint;" you are basically saying "hey unity... here's a Transform." The error message you're getting from Unity is basically saying "Yeah, that's nice, what do you want to do with it?"

    In addition to this, you're technically defining the variables only when you hit the "Builder" button, which technically means that the variables don't exist when you hit the "Cook" or "Hunter" buttons. Javascript on PC/Mac will assist you with this problem, but it's still good practice to define your variables outside of any curly brackets that are referencing them.

    I've made this change along with several other comments here, hopefully this helps:
    Code (csharp):
    1. function OnGUI() {
    2.     //I'm assuming you have cont and Skin defined in your version of the script
    3.     //It would be better to handle your length check like this:
    4.     if (Skin.Length > 0) {
    5.         GUI.skin = Skin[cont%Skin.Length];
    6.     } else {
    7.         Debug.LogError("Assign at least 1 sking on the array");
    8.         return;
    9.     }
    10.    
    11.     //Defining these variables outside of the if-statements that use them
    12.     var spawnpoints : GameObject[];
    13.     var spawnpoint : Transform;
    14.     var newTrans : Transform;
    15.    
    16.     if (GUI.Button(Rect(600,300,300,60),"Builder")) {
    17.         //You're fining all objects tagged "Spawnpoint"
    18.         spawnpoints = GameObject.FindGameObjectsWithTag ("Spawnpoint");
    19.        
    20.         Debug.Log("spawns: "+spawnpoints.length);
    21.        
    22.         //You're setting spawn point to the Transform of a randomly chosen item from spawnpoints
    23.         spawnpoint = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
    24.         //You're instantiating a builder across the network at the spawnposition's position an rotation an putting it in group 0
    25.         newTrans = Network.Instantiate(builder,spawnpoint.position, spawnpoint.rotation, 0) as Transform;
    26.        
    27.         //Presumably you're telling the game to display a string called name that I will assume you have defined in your actual script
    28.         chatScript.addGameChatMessage(name+" is a Builder");
    29.        
    30.         //You are destroying this script component from the object it's attached to
    31.         Destroy (this);
    32.     }
    33.    
    34.    
    35.    
    36.     if (GUI.Button(Rect(600,200,300,60),"Cook")) {
    37.         //These three lines are doing nothing
    38.         //You're basically saying "hey, here's a variable"
    39.         //and Unity is saying "Yeah?  And you're telling me this because?  What do you want to use them for?"
    40.         spawnpoints;
    41.         spawnpoint;
    42.         newTrans;
    43.        
    44.         //Presumably you're telling the game to display a string called name that I will assume you have defined in your actual script
    45.         chatScript.addGameChatMessage(name+" is a Cook");
    46.        
    47.         //You are destroying this script component from the object it's attached to
    48.         Destroy (this);
    49.     }
    50.    
    51.     if (GUI.Button(Rect(600,100,300,60),"Hunter")) {
    52.         //Same as above:
    53.         //These three lines are doing nothing
    54.         //You're basically saying "hey, here's a variable"
    55.         //and Unity is saying "Yeah?  And you're telling me this because?  What do you want to use them for?"
    56.         spawnpoints;
    57.         spawnpoint;
    58.         newTrans;
    59.        
    60.         //Presumably you're telling the game to display a string called name that I will assume you have defined in your actual script
    61.         chatScript.addGameChatMessage(name+" is a Hunter");
    62.        
    63.         //You are destroying this script component from the object it's attached to
    64.         Destroy (this);
    65.     }
    66. }
     
    Last edited: Sep 15, 2011