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
0
Question by Robomaster · Jan 16, 2013 at 01:03 AM · guierrorbuttonbool

Bool script error

im having alittle trouble with this script ive typed up. So what its suppose to do is that when you press equiped items button if the current target is mage its suppose to set mage to true. And then if mage is true its suppose to activate the MageEquipedItems script. But heres the problem. First When i target the Mage it doesnt set mage to true, and then the second problem is no matter whats selected if i click equiped items its forcefully sets mage to true even if warrior is selected. Could you help me find out what i did wrong thats in advance!!

 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 GameObject Mage;
     public GameObject Warrior;
    public bool mage = false;
     
 
     // Use this for initialization
     void Start () {
         mage = false;
     
     
     }
     
     // Update is called once per frame
     void Update () {
         if(mage == true){
         GetComponent<MageEquipedItems>().enabled = true;
         }
     
         
     
     }
     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;
         }
         if(button2Clicked)
             GUI.color = Color.red;
                 else
             GUI.color = Color.white;
         
         if (GUI.Button(new Rect(500,400,100,20), "Equiped Items")){{
             button1Clicked = false;
             button2Clicked = true;
             button3Clicked = false;
             button4Clicked = false;
         }
             GetComponent<Inventory>().enabled = false;
             mage = GameObject.Find("Mage");
             if(target == mage){
                 mage = true;
             }
             
             
             
             
             
         }
         if(button3Clicked)
             GUI.color = Color.red;
                 else
             GUI.color = Color.white;
                 
         if (GUI.Button(new Rect(600,400,100,20), "Stats")){{
             button1Clicked = false;
             button2Clicked = false;
             button3Clicked = true;
             button4Clicked = false;
         }
             GetComponent<Inventory>().enabled = false;
             GetComponent<MageEquipedItems>().enabled = false;
         }
         if(button4Clicked)
             GUI.color = Color.red;
                 else
             GUI.color = Color.white;
         
         if (GUI.Button(new Rect(700,400,100,20), "?")){{
             button1Clicked = false;
             button2Clicked = false;
             button3Clicked = false;
             button4Clicked = true;
         }
             GetComponent<Inventory>().enabled = false;
             GetComponent<MageEquipedItems>().enabled = false;
         }
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by iwaldrop · Jan 16, 2013 at 10:44 PM

Do you assign 'target' in the inspector? If you don't then your test for 'target == mage' will always be false.

Also, instead of setting the Component to true every frame, you should probably just do it once; when needed.

 GetComponent<MageEquipedItems>().enabled = target == mage;
Comment
Add comment · Show 9 · Share
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 Robomaster · Jan 16, 2013 at 11:21 PM 0
Share

Thanks for responding! The target gets assigned when my targeting script targets it. It shoots a raycast and when it hits a object with the "Hero" tag that object gets selected. Currently there are 4 different objects that have the hero tag. So thats y im trying to figure out how to tell the script if this certain script was selected then hence the "target == mage" but i that might be the wrong code lol. But its setting my component to true every frame how?

avatar image iwaldrop · Jan 16, 2013 at 11:28 PM 0
Share

Ok. So you're going to want to get the Hero Component (or whatever is special about heros) and query it as to whether it's a mage or not. It might look something like the following:

 YourComponent c = hitInfo.collider.gameObject.GetComponent<YourComponent>();
 if (c.type == mage)
 {
     // do something magelike
 }
avatar image Robomaster · Jan 16, 2013 at 11:31 PM 0
Share

Hero h = hitInfo.collider.gameObject.GetComponent();

if (h.type == mage)

{

GetComponent().enabled = true;

}

if that right?

avatar image iwaldrop · Jan 16, 2013 at 11:46 PM 0
Share

Very nearly. You need to specify the type with GetComponent.

 Hero h = hitInfo.collider.gameObject.GetComponent<Hero>();
 if (h is Hero)
     GetComponent<$$anonymous$$ageEquippedItems>().enabled = h.type == mage;

Of course 'h.type' is a generic reference to whatever in the Hero class defines it as a mage ins$$anonymous$$d of another type of Hero. So check whatever you use to set it to a mage and you should be good.

Also, maybe you could accept this answer if it works for you, or else it will forever be flagged as unanswered! Thanks!

avatar image Robomaster · Jan 16, 2013 at 11:58 PM 0
Share

So should i add more tags in my targeting script so that it targets more than just things with the Hero tag? So it can see what type of hero it is?

Show more comments

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

9 People are following this question.

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

Related Questions

Gui Button Turn Off Object on Click 0 Answers

2 Bugs in my game: mouse control takes some time to load and Menu not working. 0 Answers

'Button' is not a member of 'GUI' 1 Answer

Button is not a member of GUI? 2 Answers

how can i destroy a cube when i press a gui button??? 1 Answer


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