Search Unity

Resolved Changing Prefab Color

Discussion in 'Scripting' started by Tukayoo, Sep 14, 2020.

  1. Tukayoo

    Tukayoo

    Joined:
    Aug 24, 2020
    Posts:
    8
    So I am having some issue trying to get it so that when I touch/mousedown that an instantiated prefab changes color and then with mouse down still any other prefab that mouseenters, that prefab should change color as well. the bit of code that i have is shown below. Nothing happens though, other than the one prefab that received the OnMouseDown changes color, but not the others OnMouseEnter. If i remove the if statement though, they do change color OnMouseEnter. Any suggestions on how to do this or what i may be doing wrong?

    Thanks

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Letter : MonoBehaviour
    6. {
    7.     Color clkColor = Color.red;
    8.  
    9.     private bool _clicked;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         _clicked = false;
    15.     }
    16.  
    17.  
    18.     void OnMouseDown()
    19.     {
    20.         _clicked = true;
    21.         this.GetComponent<SpriteRenderer>().color = clkColor;
    22.     }
    23.  
    24.     private void OnMouseEnter()
    25.     {
    26.         if (_clicked == true)
    27.         {
    28.             this.GetComponent<SpriteRenderer>().color = clkColor;
    29.         }
    30.  
    31.     }
    32. }
     
    Last edited: Sep 14, 2020
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
    If you read up on OnMouseDown, it says it's only called on the collider you clicked on. Thus, if you have 5 objects in the scene and you click on one of them, only that object will trigger. Thus, that object will now have it's bool set to true. The other 4 do not, so moving into the other objects with OnMouseEnter will have clicked as false.

    You could make clicked static if you want it to be across all objects with this script, but if you simply want to know if the button is held down, there are ways to use the Eventsystem to check if the mouse button has been clicked or not and probably held down as well.
     
  3. Tukayoo

    Tukayoo

    Joined:
    Aug 24, 2020
    Posts:
    8
    ok thanks, i will check into making a static variable. so the private variable is for the object not just scope of the script that i am setting it in? If the static variable does not work i will check into the Eventsystem and see what that will do for me.

    Thanks
     
  4. Tukayoo

    Tukayoo

    Joined:
    Aug 24, 2020
    Posts:
    8
    Awesome that works. just changed the private bool to a public static bool and it works like a charm.

    Thanks
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Yep, non static belongs to a single instance of a script, so changing it on one copy doesn't change it for the other copies. Useful for if you had a bunch of goblins and were dealing damage to one. HP would need to be non static.

    If you make it static, it belongs to the class itself. So you can change the value and all copies of the script will have the new value. This can be useful for settings where you want all font to be the same or have the same color, for example.
     
    Tukayoo likes this.