The question is answered, right answer was accepted
Enable Image
Okay, as no one understood my recent question, i am going to be short and precise:
How do i enable an UI Image via script?
Because i have tried everything, and i can't make this work! And why is there a script in both the wiki AND the documentation that literally doesn't work? that's it, thanks.
if you found a script in the wiki & docs, why not share with everyone so it can get fixed?
where's the script that you've tried? you're more likely to be helped if you show that...
Because the last time i did that, no one even tried to edit. Also, the Unity documentation can only be edited by it's webmasters. And probaly the same applies to the wiki.
Anybody can edit a wiki, that's kind of tbe definition. Only Unity Tech can edit the docs... but how will they know that it needs editing if nobody points out mistakes?
Enable the component or set active the gameobject or both. (In case your image is not enabled and you activate gameobject, theres nothing. And if you enable image while gameobject is not active, theres nothing.)
myImage.enabled = true;
myImageObject.SetActive(true);
I did that
#pragma strict
var Img : UI.Image;
function OnTriggerEnter (other:Collider)
{
Img.enabled = false;
Img.SetActive(true);
}
And nothing happens. I get the error is not a member.
I don't understand what you are saying and I don't know how you tried to do it. You should share more about whats going on, like full error messages.
Does this change anything? var Img : UnityEngine.UI.Image; ??
Answer by Zitoox · Oct 19, 2016 at 08:26 PM
After days trying to reach an answer and failing miserably, i finally fixed it BY MYSELF. I needed to ask the same thing almost 3 times in different ways because no one even try to read my questions and already start to insulting me. I was literally insulted 2 times and all my comments were negativated.
Keeping your ignorance aside, i am going to paste here the fixed code.
#pragma strict
var Img : UI.Image;
function Start()
{
Img.enabled = false;
}
function OnTriggerEnter (other:Collider)
{
Img.enabled = true;
if (Time.timeScale == 1.0)
Time.timeScale = 0.0;
else
Time.timeScale = 0.0;
}
function Update () {
if (Input.GetKeyDown ("p")) {
if (Time.timeScale == 0.0)
Time.timeScale = 1.0;
else
Time.timeScale = 1.0;
Img.enabled = false;
}
}
This code need to be inserted on a gameobject with a collider and the "is trigger" checkbox enabled. Just drag an UI Image to the Img variable and that's it. When the player hits the gameobject's collider, it will show the UI Image and completely pause the game. Also, the script adds a unpause function to the game. In my case, i already had a pause function, so this works fine. To unpause the game and resume playing, just press P. The game will be resumed and the image will disappear.
UPDATED FIXED WORKING.
@Zitoox I'm glad you got it working, but if your question is "How do i enable an UI Image via script", you can't espect us to tell you how to get an OnTriggerEnter function to work. Also tell it if you're working in javascript, the syntax is different from real code.
P.S. don't work in javascript
I am sorry, i was really angry because my engine was bugging and i couldn't make anything work correctly, and some people were annoying me so much that i didn't even noticed that i didn't put these basic things. But i think it wouldn't change that much because i already had a question a day before that explained everything, ahh i just don't know. Anyway, i am so happy that i finally made the script work! I was trying to make a thank you letter appear to the players, it's my first game ^.^
Why don't work with javascript? =|
It's starting to go the same route as actionscript (not as bad, but it's getting there).
Also, even though the differences are not the biggest, it's still better to learn a C language for when you go to other engines later on (even tough Unity does 70%).
The first thing my professors on my developing study was: "It's good to know a lot of languages, but we are not going to $$anonymous$$ch you javascript. You are not going to use that".
No one insulted you. Don't take this as an insult. If you would have posted the code you tried, the error you got and reference to the documentation that was not working, you would have had a solution within few $$anonymous$$utes. If you post just one line of code and say: "i still get the "is not a compoent" thing." and the error actually comes from another line no one can tell the answer. Then you call me an idiot, which is an actual insult, because you didn't even show me the line that caused the error, then you ninja edit it, still refusing to tell the error and expect me to write a script to see the error. In my first comment I told you how to enable UI image, which was correct answer for your short and precise question.
I am talking about the other questions, and don't complain about it because you were rude. and i did show the image but you said that you couldn't see it. C'mmon, how are you expecting me to show an image AND to REPOST IT BECAUSE YOU CAN'T SEE IT? And i said the error like 6 times! And it was probally you that negativated my comments JUST BECAUSE you didn't even know what you was doing! And i warned that i was enraged. danidvdwerf made a script to show me, and that helped me to understand more about what was wrong. You just literally made two lines that doesn't even worked and failed at being an example.
Who down voted me and gave upvote to this? You seriously claim that these don't work? myImage.enabled = true; myImageObject.SetActive(true);
Answer by danivdwerf · Oct 18, 2016 at 08:20 AM
you should try this:
using UnityEngine;
using System.Collections;
public class OHWOW : MonoBehaviour
{
[SerializeField]private GameObject myImage;
private void Start()
{
myImage.setActive(false);
}
private void MakeItVisible()
{
myImage.setActive(true);
}
}
Good Luck!
Thanks, but i figured out that Ui Images doesn't work with set active, just with enable.
That's why I put GameObject ins$$anonymous$$d of UIImage ;)
I already set it as Image and not as Gameobject because i need to set this size... .enabled does not working if i re-declare it as a gameobject too its ok to use .setactive
realy we need some lucky to work with unity...