I'm trying to change the color of an image when I hover over it. It runs PointerEnter when I hover over it, and PointerExit when my mouse goes away.
Instead of changing the color of just the image I'm hovering over, it changes the color of every image in the scene. Can anybody help?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class click : MonoBehaviour {
void Start(){
}
void Update(){
}
public void PointerEnter(){
gameObject.GetComponent<Image>().material.color=new Color(1,1,0);
}
public void PointerExit(){
gameObject.GetComponent<Image>().material.color=new Color(1,1,1);
}
}
The material is shared for optimisation, it's the same one used for all the images using it.
You need to change the color of the image, simply remove material on your lines.
gameObject.GetComponent<Image>().color=new Color(1,1,0);
The Button component has transition settings where you can set colors for state, maybe you dont need the extra code.
Unity has 2 pre-programmed functions called OnMouseOver() and OnMouseExit(). Which you can use to see if the mouse entered your Image.
You would need to add this Code Snippet to each Image you want the Color to be changed on Hover and make sure that you get the .Color Component instead of the .material.color Component.
Example:
public class OnMouseOverExample : MonoBehaviour
{
void OnMouseOver() {
// Executes when you Hover over this GameObject with the Mouse
gameObject.GetComponent<Image>().color = new Color(1f, 1f, 0f);
}
void OnMouseExit() {
// Executes when you no longer Hover over this GameObject with the Mouse
gameObject.GetComponent<Image>().color = new Color(1f, 1f, 1f);
}
}
If you want to change a UI Components Color, you could also use the Button Component. Which already has functionality built in, you can use to tint the Color of your Image.
Related
i'm a new to unity and trying to change a sprite's color through a script.
i'm following this tutorial page: https://www.tutorialspoint.com/unity/unity_coroutines.htm. i'm using Unity 2019.2.0f1
when pressing "play" i can see the colors changing every second in the inspector, but nothing happens in the scene
the code:
public class colorChanger : MonoBehaviour
{
public int seconds_interval;
public SpriteRenderer sr;
public Color color_1;
public Color color_2;
IEnumerator changeColor(){
while (true){
if (sr.color == color_1){
sr.color = color_2;
} else {
sr.color = color_1;
}
yield return new WaitForSeconds(seconds_interval);
}
}
// Start is called before the first frame update
void Start()
{
sr = gameObject.GetComponent<SpriteRenderer>();
StartCoroutine( changeColor() );
//sr.color = color_1;
}
// Update is called once per frame
void Update()
{
}
}
explanation and screenshots:
this is the sprite, notice it starts as purple, then the script should change it's color:
https://i.ibb.co/rQy2Lpr/1.jpg
when pressing "play" i can see the colors changing every second in the inspector, but nothing happens in the scene:
yellow:
https://i.ibb.co/DRhcpPT/2.jpg
green (after 1 second):
https://i.ibb.co/Yc0v26K/3.jpg
actually, the inspector is totaly ignored by the scene! notice that the sprite is white (and not purple/yellow/green).
notice that if i change the scale nothing happens in the "play" screen:
https://i.ibb.co/sjdCvfw/4.jpg
but when i go back to "game" screen (without stopping the game), the frame of the sprite changes but not it's actual size:
https://i.ibb.co/R054Rv0/5.jpg
it looks like the script is making the inspector disconnect from the scene.
is it a bug? or am i missing something here?
thank you :)
solved!
my problem was that: I have chosen colors in the UI interface while their Alpha property was 0.
so i couldn't see any color changing.
I still don't understand why the "scale" property wasn't reacting to live changes in the inspector, but choosing colors with Alpha of 255 (opacity) solved everything.
I'm making some game menu in unity using UI event systems.
My intention is when I move mouse pointer over the text in game menu area,
font-size get bigger and when I move mouse pointer out, font size get back
to it's original size.
It works as I intended but there's a problem that if I move mouse pointer over the text and set my game menu deactive then active again using key press,font-size doesn't get back to it's original size,just getting bigger.
Here's my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class HandleIngameMenu : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler,IPointerClickHandler
{
private Text texts=null;
private void Start()
{
texts = GetComponentInChildren<Text>();
}
public void OnPointerEnter(PointerEventData data)
{
texts.fontSize += 3;
}
public void OnPointerExit(PointerEventData data)
{
if (this.gameObject.name.Equals("ReStartBtn"))
{
texts.fontSize = 30;
}
else
{
texts.fontSize = 37;
}
}
I guess setting object deactive is not same as moving mouse pointer out.
Is there any way to solve this problem??
When you activate your menu set the defaults, this way it won't matter what happens when it is deactivated. Or alternatively when you deactivate your menu run the defaults method first then deactivate. This way when it's enabled it will be back to normal.
I'm trying to apply a script that changes the material color when the cursor is on top of the object. Here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColor : MonoBehaviour {
public Color startColor;
public Color mouseOverColor;
bool mouseOver = false;
void OnMouseEnter(){
Debug.Log("START");
mouseOver = true;
GetComponent<Renderer>().material.SetColor("_Color",mouseOverColor);
Debug.Log("TESTE");
}
void OnMouseExit(){
mouseOver = false;
GetComponent<Renderer>().material.SetColor("_Color", startColor);
}
}
This code works perfectly when applied to a cube created with unity, but when I try to use it on a imported mesh, it doesn't work.
Here's an example of one of the imported objects where the script doesn't work:
Can someone help me understand how can I solve this?
Thank you
Your imported meshes don't have a collider yet which is required for the mouse detect.
Add one, for a simple mesh like a sphere just use a Sphere Collider.
Edit:
Also, you should usually store a reference if you need to access it frequently.
private Renderer rend;
private void Awake()
{
rend = GetComponent<Renderer>();
}
This question already has answers here:
How to detect click/touch events on UI and GameObjects
(4 answers)
Closed 5 years ago.
Hi as you can tell i am incredibly new to unity.
I just want a sprite to change to a different image when the mouse enters or leaves the sprite. Example, when hovering over a play button, it changes to a slightly different coloured sprite.
Thanks
Here is what i tried
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseOver : MonoBehaviour
{
public Sprite sprite1; // Drag your first sprite here
public Sprite sprite2; // Drag your second sprite here
private SpriteRenderer spriteRenderer;
void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
if (spriteRenderer.sprite == null)
spriteRenderer.sprite = sprite1;
}
void OnMouseEnter()
{
ChangeSprite();
}
void OnMouseExit()
{
ChangeSprite();
}
void ChangeSprite()
{
if (spriteRenderer.sprite == sprite1)
{
spriteRenderer.sprite = sprite2;
}
else
{
spriteRenderer.sprite = sprite1;
}
}
}
I would prefer to use UI Button with Image component. It has transition property where you can use different sprites on different states (normal, hover, pressed and disabled states).
Just use Button component and set transition mode to SpriteSwap:
Button Transition property should look like this:
As Default button object has image in a child component which is set to Target Graphic, you can set sprite1 as image source by default and use sprite2 as Highlighted Sprite.
This would work perfectly without using any code.
Hope this helps :)
Your sprite needs a collider attached to it in order for the OnMouseEnter() event to fire.
I want to change the UI image in random order. I have a gameobject in UI(canvas) containing Image component and it has null image initially. I have a script attached to it(gameobject) to change the image on run time.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class changeImg : MonoBehaviour {
public Sprite sprite1;
public Sprite sprite2;
public Sprite sprite3;
void Start()
{
ChangeImg();
}
void ChangeImg()
{
int rand=Random.Range(0,3);
if(rand==0)
{
gameObject.GetComponent<Image> ().sprite = sprite1;
//gameObject.GetComponent<UnityEngine.UI.Image> ().sprite = sprite1;
}
else if(rand==1)
{
gameObject.GetComponent<Image> ().sprite = sprite2;
// gameObject.GetComponent<UnityEngine.UI.Image> ().sprite = sprite2;
}
else if(rand==2)
{
gameObject.GetComponent<Image> ().sprite = sprite3;
//gameObject.GetComponent<UnityEngine.UI.Image> ().sprite = sprite3;
}
}
}
I have assigned the public field (sprite1,sprite2,sprite3) in inspector. And I tried the both option as I had commented in code. I did not get an error but also the image did not get change as I want. During runtime of a program, GameObject(to which the script is attached) has null image source as it was initially.
Use overrideSprite field instead of sprite - documentation
Unfortunately, unity ui is full of such pitfalls and it's api is totally counter-intuitive, so you have to be careful and check the docs regularly
You can also just use Image if you are in Unity 2017.3 (not sure if this works for older versions). For example:
using UnityEngine.UI;
-----
public Image ObjectwithImage;
public Sprite spriteToChangeItTo;
void Start () {
ObjectwithImage.sprite = spriteToChangeItTo;
}
Works great for me.
Have you checked the position of the Gameobject? Also the color of the image?
To change the Image from a Button, don't use GetComponent<Image> () as you can potentially get another Image component that does not belong to the button. It can also return null if the object is disabled.
Use the Button.image.sprite or Button.image.overrideSprite variable instead.
public Button pb;
public Sprite newSprite;
void Start()
{
pb.image.sprite = newSprite;
}
Or
pb.image.overrideSprite = newSprite;
It really doesn't matter which one is used. Any of these two should work.