Problem with Swap Image in Unity 5 with C#.
Hi everyone.
I my demo, I need when the player enter a “trigger”, the UI image on screen change from one image to other, something like “you crossed a line” sort of. The problem is I don’t know how to do that by C# script, using the new Canvas.
In older versions of Unity, that was easy using GUI Texture, but this tool seems no working on Unity 5 any more. I google, I dig in forums over the net, but the few info available is confusing and non-existen samples to how manipulate images-canvas with code.
Hope someone help. What should be a simple task, its been a NIGHTMARE for almost a week and keep me stuck.
void OnTriggerEnter(Collider other) { if(other.tag == "Player") { print("Tank has crossed"); //show new image intead of the older one. }
}
}
Answer by Abhiroop-Tandon · Mar 22, 2016 at 02:19 PM
Just use another UI image at the exact same place and when you enter the trigger just set the current one to SetActive(false) and the other one to SetActive(true). And when you exit the trigger you can do something similar to go back.
Answer by SalDeveloper73 · Mar 23, 2016 at 08:42 AM
Hi Abhiroop Tandon.
In fact, using "SetActive(true)" didn´t work either. Not sure why, maybe Unity doesn´t recognize canvas-image as GameObject. This is what finally works well:
public Sprite miImageTwo; public Image imageUI;
void OnTriggerEnter(Collider other) { if(other.tag == "Player") imagenUI.sprite = miImageTwo; }
In perspective:
Put “UnityEngine.UI” at the top of the script (library UI).
Create the public image variable in that script.
Put the desired image in the Hierarchy by canvas.
In the GameObject in Hierarchy, drag the image-canvas to the inspector in the slot given by the script.
Test!
Thanks for your comments! :D
Can you show me your script because i tried and it is working fine with me !!
O$$anonymous$$. First In the scene, I create a image using "canvas". Then In the script I created the variables:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class ChangeImage : $$anonymous$$onoBehaviour {
public Image imagenUI; public Sprite myImage;
void OnTriggerEnter(Collider other) { if(other.tag == "Player") { imagenUI.sprite = myImage; }
}
}
Having the script, we create an GameObject in which to attach it. In Inspector Unity I drag the image-canvas to the slot called "myImage" of that GameObject with the script.
Last, we put a GameObject - no doubt the final player - an insert a RigidBody. $$anonymous$$y player will have a tag named "Player" and test the file. It must be avaibable ir order to move.
¿Can I have a look at yours?
What i do is after creating a canvas i add two images from the UI submenu, rename them as something relevant, anchor them at the bottom left corner. Then in the script i just find these two images and enable and disable as i wish. Looks something like this
GameObject background1 = GameObject.Find("Background1");
GameObject background2 = GameObject.Find("Background2");
background1.SetActive(false);
Guauh... works like a charm!
Thanks a lot.
PS: sorry for the late answer, I´m also checking some stuff with Unity, Augment Reality and Android. It´s a hell of work.