Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 jammachiq · Aug 06, 2015 at 02:36 AM · uibuttonmaterialbooleanbuttons

Changing Color of Material with Bool and Unity UI

So I am running in to a weird problem using Unity UI system in the Inspector. What I want to do is to be able to press the Move Or Attack button and have the highlights over the tiles respond so that when the player presses the Move button, the tiles change color to blue and when they press the Attack button the tiles change to red. The color change will only happen when the mouse is hovering over the tiles. On mouse exit they are clear. I thought that this would work.

 public class ClickableTile : MonoBehaviour {
     //private UnitMovement unitActions = new UnitMovement ();
     public int tileX;
     public int tileY;
     public TileMap map;
     public bool characterisString;
     public bool attacking;
     public bool moving;
 
     void Start()
     {
         
         characterisString=true;//unitActions.moving=true;
     }
 
     void OnMouseUp()
     {
         Debug.Log ("Click");
         map.MoveSelectedUnitTo (tileX, tileY);
     }
 
     void OnMouseEnter() {
         if (moving=true) {
             transform.GetComponent<Renderer>().material.color = Color.blue;
             Debug.Log("Blue");
         } else if (attacking=true) {
             transform.GetComponent<Renderer>().material.color = Color.red;
             Debug.Log("Red");
         }
         //Debug.Log("my position is (" + gridPosition.x + "," + gridPosition.y);
     }
     void OnMouseExit()
     {
         transform.GetComponent<Renderer>().material.color = Color.white;
     }
 
     public void AttackButton()
     {
         if (!attacking) {
             moving = false;
             attacking = true;
         } else {
             moving = false;
             attacking = false;
         }
     }
     public void MoveButton()
     {
         if (!moving) {
             moving = true;
             attacking = false;
         } else {
             moving = false;
             attacking = false;
         }
     }
 
 }

However, what ends up happening is that the color of the tiles just stay as blue.I currently have the AttackButton attached to the OnClick() for the Attack button and the MoveButton() attached to the OnClick() for the Move Button. Here is a screen shot of the scene.

http://gyazo.com/563cf0a0ad3155d46ddcf212a090b901

If any one can help me out, I would greatly appreciate it.

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
1
Best Answer

Answer by allenallenallen · Aug 06, 2015 at 06:27 AM

Think about how the whole process should execute first.

  1. Mouse enters tile

  2. Mouse click

  3. Registers as attacking or moving or whatever

But the way you do it is like this.

  1. Mouse click

  2. Registers as attacking or moving or whatever

  3. Mouse enters tile

You see the order is wrong.

When you do OnMouseEnter, you haven't decided on whether the player is attacking or moving yet so it won't work.

So in order to fix this:

I suggest you delete OnMouseEnter because you don't need it. Instead, put everything in OnMouseEnter to MoveButton before the if statements.

  public void MoveButton()
  {
      if (moving=true) {
          transform.GetComponent<Renderer>().material.color = Color.blue;
          Debug.Log("Blue");
      } else if (attacking=true) {
          transform.GetComponent<Renderer>().material.color = Color.red;
          Debug.Log("Red");
      }
      //Debug.Log("my position is (" + gridPosition.x + "," + gridPosition.y);
 
      if (!moving) {
          moving = true;
          attacking = false;
      } else {
          moving = false;
          attacking = false;
      }
  }

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

This is not possible to be called for standalone input. Please check your platform and code where this is called (C#) 2 Answers

Detect if player wants to pause or to jump 3 Answers

UI button material shows up black? 1 Answer

change color with playerprefs using buttons, between scenes.,How to use playerprefs to save colors from a button? 3 Answers

How to make UI buttons so my player can move along certain lines 0 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