Author Topic: [2.x] How to change the color of an object when tapping it  (Read 2185 times)

Offline William Ravaine

  • Administrator
  • Regular Poster
  • *****
  • Posts: 657
  • Karma: +27/-2
    • View Profile
    • Fatal Frog Software
[2.x] How to change the color of an object when tapping it
« on: 15 July, 2012, 03:30:52 PM »
Here's a mini-tutorial on how to make a particular object in your scene (e.g. a "Crate" object) change color when the user taps on it. We will use the Toolbox's TBTap script for this.

1) Make sure you have one instance of the "Finger Gesture Initializer" prefab in your scene
2) Create a new empty GameObject, call it "Toolbox InputManager" and add the TBInputManager script to it
3) Put the TBTap script on your Crate object
4) Create a new "ChangeColorOnTap" script (or whatever you want to call it) and add the following code to it:

Code: [Select]
void OnTap()
{
    Debug.Log( "Tapped " + this.name );

    // pick a random color
    Color newColor = new Color( Random.value, Random.value, Random.value, 1.0f );

    // apply it on current object's material
    renderer.material.color = newColor;
}

5) Add our new ChangeColorOnTap script to your Crate object
6) Hit play and tap away! The crate should change to a random color every time you tap on it.
« Last Edit: 17 March, 2013, 11:01:05 AM by William Ravaine »

Offline vickygroups

  • FingerGesturer
  • Recruit
  • ****
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: [2.x] How to change the color of an object when tapping it
« Reply #1 on: 16 April, 2014, 06:18:09 PM »
Thank you for the post.  Two questions:

- there's no longer a script called TBTap.  Do you mean put in the tap recognizer?
- what if you don't want a random color, but want to cycle through a preset group of colors?