- Home /
              This question was 
             closed Apr 28, 2014 at 03:38 AM by 
             SteelArrow21 for the following reason: 
             
 
            The question is answered, right answer was accepted
 
               Question by 
               SteelArrow21 · Apr 28, 2014 at 02:41 AM · 
                c#2d  
              
 
              GetComponent().enabled = true;
I have this line of code:
 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
      private GameObject weapGun;
      private GameObject weapMelee;
 
      void Awake(){
 
           weapGun = GameObject.FindGameObjectWithTag("Gun");
           weapMelee = GameObject.FindGameObjectWithTag("Melee");
           Debug.Log(weapGun);
           Debug.Log(weapMelee);
 
      }
      void Update(){
 
           if(Input.GetKeyDown("1")){
                weapGun.GetComponent<SpriteRenderer>().enabled = true;
                weapMelee.GetComponent<SpriteRenderer>().enabled = false;
           }
 
           if(Input.GetKeyDown("2")){
                weapGun.GetComponent<SpriteRenderer>().enabled = false;
                weapMelee.GetComponent<SpriteRenderer>().enabled = true;
           }
      }
 }
I get no errors and the Debug.Log()'s return the correct GameObjects, but the "GetComponent().enabled = true/false; are not doing anything? Can anyone please point out my error.
               Comment
              
 
               
              Answer by Vandarthul · Apr 28, 2014 at 03:16 AM
I believe the problem is GetComponent returns a Type therefore it needs to be assigned to a variable.
 SpriteRenderer weapGunRenderer = weapGun.GetComponent<SpriteRenderer>();
 weapGunRenderer.enabled = true;
 SpriteRenderer weapMeleeRenderer = weapMelee.GetComponent<SpriteRenderer>();
 weapMeleeRenderer.enabled = false;
I still seem to be getting the same outcome. I think both of ours mean the exact same thing anyways.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# scriping help 0 Answers
Inventory system Help 2 Answers
How To Make An Invisible Wall That's Impossible To Get Out Of? 1 Answer
Tutorial level 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                