- Home /
works with gameObject but not with Image SetActive vs Enabled
so when i use this code it works perfectly on SetActive for gameObjects instead of Enabled for UI Images.
I would love to just use gameObjects but they don't scale well with the UI elements and I can't seem to get the gameobjects to anchor properly. (i will gladly accept an answer to this instead of the following if it's easier).
it's disabling the 50 image on press but it's not enabling the 200 image.
what is wrong with my code that is not enabling the "200" element
public void BetterShield() { if (ShopUIManager.shopUIManager.ShieldUpgrade50Vis == true) {
ShopUIManager.shopUIManager.ShieldUpgrade50Vis = false;
ShopUIManager.shopUIManager.ShieldUpgrade50.enabled = !ShopUIManager.shopUIManager.ShieldUpgrade50.enabled;
ShopUIManager.shopUIManager.ShieldUpgrade200Vis = true;
ShopUIManager.shopUIManager.ShieldUpgrade200.enabled = !ShopUIManager.shopUIManager.ShieldUpgrade200.enabled;
}
}
}
Why can't you SetActive on your UI elements? They are gameObjects. The UI images only difference is they use a rect transform. $$anonymous$$aybe I am misunderstanding.
so if I use an image and I do the logic on that last part, where it currently says "enabled", if I try and put the line "SetActive" it comes back as an invalid line. so I assume if I have an Image I cant use attractive. but if I try to do the gameobjects when its built and put on a device other than the editor it's not in the right place, because the gameobjects dont have anchors that I can use on them.
Right, because you need to call it like this:
ShopUI$$anonymous$$anager.shopUI$$anonymous$$anager.ShieldUpgrade50.gameObject.SetActive(false);
Because an image is on a gameObject.
will that method solve the location problem when I make it as a gameobject or the inactive to active problem when I make it as a UI image.
This will fix your inactive/active problem.
I still feel you may be confused about a UI image and a gameObject. When you created the UI image, it creates a gameObject and puts an image component on it. Thus, anything in the scene is going to be a gameObject.
ok, SO, you solved it. It didn't help that I'm new and a few things weren't clicking until you made you last point.
I didn't realize that even tho i made a UI Image it still counts as a game object, so in my script i was able to declare it as a game object and place the image inside that part of the editor.
when i first did "create empty" and made that the gameobject with the sprite i couldn't anchor on the same scales as the other UI elements which is what i really needed for consistency.
now that i know i can declare a game object in the script and a Ui Image in the canvas and place the image in the area for that gameobject it made it very easy to setactive to false or true.
you're awesome. would you $$anonymous$$d retyping your answer as an answer, it's not letting me reward you and this was super helpful.
No problem. We all are learning. :)
Just as more knowledge, UI element is special in that it uses a rect transform ins$$anonymous$$d of a normal transform. All UI elements us this special transform. It has all the functionality as a transform (it derives from it), but it allows you to use anchors and such. Helps make sure everything is scaled properly in the UI canvas.
any idea why when i build it to my android it doesn't function like it does in the editor?
I would delete the app. $$anonymous$$ake sure everything is indeed updated in your scene. Then create another build. $$anonymous$$aybe the project didn't save or was in a weird state and didn't get your changes.
Answer by Vega4Life · Dec 11, 2018 at 03:09 AM
UI images live on gameObjects, so you can still SetActive on them. Like this:
ShopUIManager.shopUIManager.ShieldUpgrade50.gameObject.SetActive(false);
Your answer
Follow this Question
Related Questions
OnSelect and OnPointerEnter work in one script but not another on the same object 2 Answers
Calling setActive(false) on a disabled object? 3 Answers
Turning off ONE GUI button 1 Answer
how to disable a button after pressing it and enable again after pressing another button 1 Answer
Re enable the script to recover the initial variables 2 Answers