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

Instantiating Function

Discussion in 'Scripting' started by Sydious, Jul 26, 2014.

  1. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    I am using a function that contains a switch statement. Each case would instantiate a different prefab.

    I am getting the following error message in the console:
    It's the second case that is causing the issue. This much I know. I assume it's the second call of var rHand_Weapon that is the issue. My question is how do I go about this if I can't call that var in each case?

    here is the function in question.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var doDebug: boolean;
    4. var heroBody: Transform;
    5. var rightHand: Transform;
    6. var leftHand: Transform;
    7.  
    8. enum heroClasses {Warrior, Archer, Wizard, Cleric}
    9. var HeroTextures: Texture[];
    10. var weapons: GameObject[];
    11.  
    12.  
    13.  
    14. var hClass: heroClasses;
    15. var level: int;
    16.  
    17.  
    18.  
    19. function Start () {
    20.  
    21.     HeroSetup(hClass);
    22.  
    23. }
    24.  
    25. function HeroSetup(hc : heroClasses){
    26.    
    27.     if(doDebug){Debug.Log("Starting to setup a " + hClass + ".");}
    28.    
    29.     switch (hc){
    30.         case heroClasses.Warrior:
    31.             if(doDebug){Debug.Log("Putting on Armor.");}
    32.             heroBody.renderer.material.mainTexture = HeroTextures[0];
    33.            
    34.             if(doDebug){Debug.Log("Grabbing Equipment.");}
    35.             var rHand_Weapon: GameObject = Instantiate(weapons[0],rightHand.position, rightHand.rotation);
    36.  
    37.         break;
    38.        
    39.         case heroClasses.Archer:
    40.             if(doDebug){Debug.Log("Putting on Armor.");}
    41.             heroBody.renderer.material.mainTexture = HeroTextures[1];
    42.             if(doDebug){Debug.Log("Grabbing Equipment.");}
    43.             var rHand_Weapon: GameObject = Instantiate(weapons[1],rightHand.position, rightHand.rotation);
    44.         break;      
    45.    
    46.     }
    47.  
    48.     if(doDebug){Debug.Log("Setup Complete.");}
    49. }
    50.  
    I want to be able to access that Game Object again such as
    Code (JavaScript):
    1. rHand_Weapon.transform.renderer.material.color = Color.black
    and the such....
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Change the second "var rHand_Weapon: GameObject = " to just "rHand_Weapon = ", since you already defined it earlier. However I don't think you need all that; just do:

    Code (csharp):
    1. function HeroSetup(hc : heroClasses){
    2.     heroBody.renderer.material.mainTexture = HeroTextures[hc];
    3.     var rHand_Weapon: GameObject = Instantiate(weapons[hc],rightHand.position, rightHand.rotation);
    4. }
    This assumes the heroClasses enum matches up with the arrays.

    --Eric
     
  3. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    I did try removing var from rHand_Weapon but it threw a different error about it being unknown. That's where I got real confused and posted.

    I was going to use the simple approach you pointed out, although I don't think the heroClasses enum will always match up so i was using the switch to deal with the differences. A lot more is planned for inside those case statements.

    Edit - I just took another stab at removing var and i realized I was still declaring with rHand_Weapon: GameObject instead of just setting it's value.

    oh the little things that cause big issues. Thanks for your help Eric.