Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by Robomaster · Feb 07, 2013 at 02:41 AM · guierrorgetcomponentbooldisabling

Get Component Error

Hello so the problem is Unity isn't reading the code within the second bracket for the exception of the first one. So everything including GetComponent().enabled = false; and down isn't getting executed. What the code is suppose to do is disable all the scipts within that bracket except for the inventory script which it will activate. This is so it only shows the inventory screen and not any of the other screens. When i go to test it, the inventory scripts activates but all the scripts that are suppose to become disabled stay activated. The area in the code in which im having the problem is towards the bottom it'll have *** showing that, that is the area where the script isn't working.

 using UnityEngine;
 using System.Collections;
 
 public class LowerBar1 : MonoBehaviour
 {
     bool button1Clicked = false;
     bool button2Clicked = false;
     bool button3Clicked = false;
     bool button4Clicked = false;
     public GameObject target;
     public bool mage = false;
     public bool warrior = false;
     public bool archer = false;
     public bool rouge = false;
     public int currentValue = 0;
     public Transform selectedTarget;
     public Transform selectedTarget1;
     public Transform selectedTarget2;
     public Transform selectedTarget3;
     
     void Start ()
     {
         mage = false;
         warrior = false;
         archer = false;
         rouge = false;
     }
     
     void Update ()
     {
         AdjustCurrentValue(0);
         if (Input.GetMouseButtonUp(1))
         {
             RaycastHit hit; // cast a ray from mouse pointer:
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Mage"))
             {
                 DeselectTarget(); // deselect previous target (if any)...
                 selectedTarget = hit.transform; // set the new one...
                 SelectTarget(); // and select it
             }
             if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Warrior"))
             {
                 DeselectTarget1(); 
                 selectedTarget1 = hit.transform;
                 SelectTarget1();
             }
             if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Archer"))
             {
                 DeselectTarget2(); 
                 selectedTarget2 = hit.transform;
                 SelectTarget2();
             }
             if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Rouge"))
             {
                 DeselectTarget3(); 
                 selectedTarget3 = hit.transform;
                 SelectTarget3();
             }
         }
     }
     
     private void SelectTarget()
     {
         mage = true;
         warrior = false;
         archer = false;
         rouge = false;
         GameObject.Find("Archer").GetComponent<ArcherHealth>().enabled = false;
         GameObject.Find("Archer").GetComponent<ArcherTargeting>().enabled = false;
         GameObject.Find("Warrior").GetComponent<WarriorHealth>().enabled = false;
         GameObject.Find("Warrior").GetComponent<WarriorTargeting>().enabled = false;
         GameObject.Find("Mage").GetComponent<MageHealth>().enabled = true;
         GameObject.Find("Mage").GetComponent<MageTargeting>().enabled = true;
         GameObject.Find("Rouge").GetComponent<RougeHealth>().enabled = false;
         GameObject.Find("Rouge").GetComponent<RougeTargeting>().enabled = false;
     }
     
     private void DeselectTarget()
     {
         if (selectedTarget)
         {
             selectedTarget = null;
         }
     }
     
     private void SelectTarget1()
     {
         mage = false;
         warrior = true;
         archer = false;
         rouge = false;
         GameObject.Find("Archer").GetComponent<ArcherHealth>().enabled = false;
         GameObject.Find("Archer").GetComponent<ArcherTargeting>().enabled = false;
         GameObject.Find("Warrior").GetComponent<WarriorHealth>().enabled = true;
         GameObject.Find("Warrior").GetComponent<WarriorTargeting>().enabled = true;
         GameObject.Find("Mage").GetComponent<MageHealth>().enabled = false;
         GameObject.Find("Mage").GetComponent<MageTargeting>().enabled = false;
         GameObject.Find("Rouge").GetComponent<RougeHealth>().enabled = false;
         GameObject.Find("Rouge").GetComponent<RougeTargeting>().enabled = false;
     }
     
     private void DeselectTarget1()
     {
         if (selectedTarget1)
         {
             selectedTarget1 = null;
         }
     }
     
     private void SelectTarget2()
     {
         mage = false;
         warrior = false;
         archer = true;
         rouge = false;
         GameObject.Find("Archer").GetComponent<ArcherTargeting>().enabled = true;
         GameObject.Find("Archer").GetComponent<ArcherHealth>().enabled = true;
         GameObject.Find("Warrior").GetComponent<WarriorHealth>().enabled = false;
         GameObject.Find("Warrior").GetComponent<WarriorTargeting>().enabled = false;
         GameObject.Find("Mage").GetComponent<MageHealth>().enabled = false;
         GameObject.Find("Mage").GetComponent<MageTargeting>().enabled = false;
         GameObject.Find("Rouge").GetComponent<RougeHealth>().enabled = false;
     }
     
     private void DeselectTarget2()
     {
         if (selectedTarget2)
         {
             selectedTarget2 = null;
         }
     }
     
     private void SelectTarget3()
     {
         mage = false;
         warrior = false;
         archer = false;
         rouge = true;
         GameObject.Find("Archer").GetComponent<ArcherHealth>().enabled = false;
         GameObject.Find("Archer").GetComponent<ArcherTargeting>().enabled = false;
         GameObject.Find("Warrior").GetComponent<WarriorHealth>().enabled = false;
         GameObject.Find("Warrior").GetComponent<WarriorTargeting>().enabled = false;
         GameObject.Find("Mage").GetComponent<MageHealth>().enabled = false;
         GameObject.Find("Mage").GetComponent<MageTargeting>().enabled = false;
         GameObject.Find("Rouge").GetComponent<RougeHealth>().enabled = true;
         GameObject.Find("Rouge").GetComponent<RougeTargeting>().enabled = true;
     }
     
     private void DeselectTarget3()
     {
         if (selectedTarget3)
         {
             selectedTarget3 = null;
         }
     }
     
     void OnGUI()
     {
         GUI.Box(new Rect(0,400,200,200), "");
         GUI.Box(new Rect(50,405,100,25), "GAME");
         GUI.Box(new Rect(0,430,100,25), "Game Type:");
         GUI.Box(new Rect(0,455,100,25), "Phase:");
         GUI.Box(new Rect(0,480,100,25), "Ally Heroes:");
         GUI.Box(new Rect(0,505,100,25), "Enemy Heroes:");
         GUI.Box(new Rect(0,530,100,25), "Monsters:");
         GUI.Box(new Rect(0,555,100,25), "Total Gold:");
         GUI.Box(new Rect(200,400,200,90), "");
         GUI.Box(new Rect(200,405,100,20), "Hero:");
         GUI.Box(new Rect(200,425,100,20), "Level:");
         GUI.Box(new Rect(200,445,100,20), "Health:");
         GUI.Box(new Rect(200,465,100,20), "Mana:");
         GUI.Box(new Rect(200,490,200,110), "");
         GUI.Box(new Rect(400,420,400,180), "");
         
         if(button1Clicked)
             GUI.color = Color.red;
         else
             GUI.color = Color.white;
         
         if (GUI.Button(new Rect(400,400,100,20), "Inventory"))
         {
             {
                 {
                     {
                         {
                             button1Clicked = true;
                             button2Clicked = false;
                             button3Clicked = false;
                             button4Clicked = false;
                         }
                         GetComponent<Inventory>().enabled = true;
                     }
                     GetComponent<MageEquipedItems>().enabled = false;
                     GetComponent<WarriorEquipedItems>().enabled = false;
                     GetComponent<ArcherEquipedItems>().enabled = false;
                     GetComponent<RougeEquipedItems>().enabled = false;
                 }
                 GetComponent<MageStats>().enabled = false;
                 GetComponent<WarriorStats>().enabled = false;
                 GetComponent<ArcherStats>().enabled = false;
                 GetComponent<RougeStats>().enabled = false;
             }
             GetComponent<MageAbilities>().enabled = false;
             GetComponent<WarriorAbilities>().enabled = false;
             GetComponent<ArcherAbilities>().enabled = false;
             GetComponent<RougeAbilities>().enabled = false;
         }
Comment
Add comment · Show 19
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OWiz · Feb 07, 2013 at 03:04 AM 0
Share

It's only reading the first set of brackets, which is button1Clicked = true, etc, or it's only reading up to "GetComponent().enabled = true;?

avatar image Robomaster · Feb 07, 2013 at 03:15 AM 0
Share

Sry its only reading the GEtcomponent().enabled = false one but its only reading the first one/line and none of the others

avatar image Wolfram · Feb 08, 2013 at 11:20 AM 1
Share

Uh...now you lost me. Retyping what again? I thought this was about parts of your code not getting executed. So what exactly are you editing?

avatar image Bunny83 · Feb 10, 2013 at 10:15 PM 1
Share

We still don't understand what exactly happends because what you say makes no sense. Your code is compiled into something your computer can execute. If it compiles without any errors that means the code will execute in the exact order you've arranged it.

The only thing that might interrupt the execution is when an exception occurs. This can happen when, for example, the component you try to get with GetComponent isn't attached to this gameobject. If an exception isn't handled by your code it will fall through until it reaches Unity and Unity will display an error.

That means: When you don't get an error and you don't catch the exception yourself (with a try-catch block), there's no reason which could interrupt the execution.

What Wolfram was asking have you added a Debug.Log("Yeah, i reached this line"); at the end of your code block to see it reaches the line or not?

You just posted a code fragment. We have no idea to what class this fragment belongs. It's probably inside a OnGUI function.

Furthermore your "second pair of brackets" are completely useless. I reformatted your code so it becomes a bit more clear.

It seems you have a quite complex GameObject since you have a lot scripts attached to it. We know nothing about your scene, your setup or anything about your situation except your 2.4 lines of description above.

So can you please improve your question because it's obvious that no one understand what's your problem. Feel free to edit your question and include all relevant code and give some more information what debugging steps you've already tried. We can't debug your stuff for you since we don't have your project.

avatar image Wolfram · Feb 10, 2013 at 10:49 PM 1
Share

What's with the 5 nested code blocks starting on line 75? They are unnecessary and do nothing, except making the code harder to read (especially since the rest of your indenting isn't consistent anyway).

Concerning code execution they should not matter, but your claim the code folliwing the first block doesn't get executed suggests otherwise. So remove all nested { and }.

Also, inserting Debug.Log() at key points in your code is the very first thing you should try to check whether (and when and how often) a certain code passage is getting executed or not.

Wild guess: you have other scripts or code fragments that re-enable the components you are trying to switch off, so it seems the code you posted is not getting executed while in fact it is, but it's immediately overridden by some other script or code segment.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bool script error 1 Answer

Using arrays and for loop with GetComponent error 1 Answer

help with error CS0029: Cannot implicitly convert type `bool' 1 Answer

Activating UI panel on GameObject Click/Destroy 0 Answers

Build completed with a result of failed (can't build my android game) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges