Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by SalDeveloper73 · Mar 22, 2016 at 01:46 PM · c#canvasimage

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. }

 }

} alt text

screen-tankg2.jpg (64.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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:

  1. Put “UnityEngine.UI” at the top of the script (library UI).

  2. Create the public image variable in that script.

  3. Put the desired image in the Hierarchy by canvas.

  4. In the GameObject in Hierarchy, drag the image-canvas to the inspector in the slot given by the script.

  5. Test!

Thanks for your comments! :D

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Abhiroop-Tandon · Mar 23, 2016 at 10:41 AM 0
Share

Can you show me your script because i tried and it is working fine with me !!

avatar image SalDeveloper73 Abhiroop-Tandon · Mar 24, 2016 at 08:52 AM 0
Share

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; }

 }

}

avatar image SalDeveloper73 SalDeveloper73 · Mar 24, 2016 at 09:05 AM 0
Share

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?

avatar image Abhiroop-Tandon Abhiroop-Tandon · Mar 24, 2016 at 09:34 AM 0
Share

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);


avatar image SalDeveloper73 Abhiroop-Tandon · Apr 06, 2016 at 04:45 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Changing Unity.UI.Panel Color 3 Answers

Hotbar appears when a certain gameobject is selected? 1 Answer

How to draw an Image between two objects within the canvas? 1 Answer

Change Canvas Image from Another Script 1 Answer

Scaling an image sent to UI Image 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges