Author Topic: How to properly use UITween to change color of a Sprite with NGUI  (Read 3415 times)

unitymoo

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 8
    • View Profile
Hi,

I want to change the color of a sprite on click -- to a random color from array.
When I click the sprite, the console message shows I'm getting a color from the array.
But I can't get the sprite to actually change color on-screen.
Does anyone know how to make this work properly?

Also I'm new to NGUI / coding in general so if there is an elegant solution to this problem please let me know.

Here is my code:

using UnityEngine;
using System.Collections;

public class btnUpdate2 : MonoBehaviour {

   public Color[] squareColor;

   public void onClickTweenColor () {
      
      print ("example color from array: " + squareColor [Random.Range (0, squareColor.Length)]);

      GameObject.Find ("WhiteSquare").GetComponent<UIWidget> ().color = squareColor [Random.Range (0, squareColor.Length)];
   }
}



unitymoo

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: How to properly use UITween to change color of a Sprite with NGUI
« Reply #1 on: December 24, 2016, 10:05:08 AM »
hmm... if I could figure out how to change the title of my original post
I would change it to something like:

How to properly change color of a Sprite with NGUI

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: How to properly use UITween to change color of a Sprite with NGUI
« Reply #2 on: December 24, 2016, 06:11:42 PM »
try this:

transform.FindChild("WhiteSquare").GetComponent<UISprite>().color = Color.red;

PS stay away from GameObject.Find

unitymoo

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: How to properly use UITween to change color of a Sprite with NGUI
« Reply #3 on: December 26, 2016, 02:45:19 PM »
Thanks, this worked for me.

Here's how I used your code in an on click function to change the color of a sprite

I put the sprite at the same position as my button
and changed sprite depth to 1 (above the button, which has depth 0)

since adding the button script to an object seems to disable ability to change color

   public void OnBlueBtnClicked () {
      print ("blue was clicked");
      transform.FindChild("WhiteSquare").GetComponent<UISprite>().color = Color.green; // change sprite to color red
   }

unitymoo

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: How to properly use UITween to change color of a Sprite with NGUI
« Reply #4 on: December 26, 2016, 02:55:24 PM »
Hmm.... still trying to figure out how to color sprite randomly from an array of colors

using UnityEngine;
using System.Collections;

public class BtnActionColor : MonoBehaviour {

   public Color[] myRandomSpriteColor;

   public void OnBtnClick () {
      transform.FindChild ("WhiteSquare").GetComponent<UISprite> ().color = myRandomSpriteColor [Random.Range (0, myRandomSpriteColor.Length)];
   }
}

In the Inspector I must create an array of colors

but when I test the project and click the button
the white square sprite which I have placed on top of my button just disappears
Instead I want the white square to change to a random color from the array of colors I have created...

Any other helpful ideas on how to change a white sprite to a random color from array?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to properly use UITween to change color of a Sprite with NGUI
« Reply #5 on: December 31, 2016, 07:33:42 AM »
Buttons use color tweens to change colors. If you want to change a color of a specific sprite, first you should reference it, not try to find it by name -- and second, make sure you are not already coloring it via other means (like a button). Also what's OnBtnClick? NGUI's click event is OnClick, not OnBtnClick.