- Home /
Moving a UI image by script
Hey
Right now I'm creating a menu using Unity and the UI system from 4.6. Basically I'm trying to make it so that if I press down then a certain image will move down to a specified position and if I press up it will go up again.
I've tried the GetComponent.anchoredPosition route, and I tried something like this using UnityEngine; using System.Collections; using UnityEngine.UI; public class Menu_Selection_Indicator : MonoBehaviour { Image [] images01; Image [] images02; int i = 0; void MakeActive (){ for( i = 0;i<6;i++){ %|1898136422_1|% images02[i].enabled = true; } } void Update () { images01.enabled = false; images02.enabled = false; if (Input.GetKeyDown("up")) { %|1096323275_3|% } if (Input.GetKeyDown("down")) { %|341979072_4|% } if (i>5) { %|-1306517619_5|% } if (i<1) { %|-1248293269_2|% } images01[i].enabled = true; images02[i].enabled = true; %|679944723_3|% } }
And although this isn't ideal because of the performance issues for having to turn on and off 10 different images, even this doesn't work, with me getting the errors "Assets/Scripts/CSharpScripts/Menu_Selection_Indicator.cs(41,13): error CS1061: Type UnityEngine.UI.Image' does not contain a definition for
SetActive' and no extension method SetActive' of type
UnityEngine.UI.Image' could be found (are you missing a using directive or an assembly reference?)"
and "Assets/Scripts/CSharpScripts/Menu_Selection_Indicator.cs(23,10): error CS1061: Type UnityEngine.UI.Image[]' does not contain a definition for
SetActive' and no extension method SetActive' of type
UnityEngine.UI.Image[]' could be found (are you missing a using directive or an assembly reference?)"
So my question is does anyone know of a way where I could just move my UI image's anchored position from one Vector to another.
Thank you
Dok
Answer by palash_bhowmick1 · Aug 27, 2015 at 09:27 AM
There is no SetActive in your script... So how can it give according to the script you posted...
If you want to deactivate anything, you need to deactivate the gameObject not the component.. For example,
images01[1].gameObject.setActive(false);
this will work
Thank you for your answer
The question however is how I could move it using the anchoredPosition $$anonymous$$ethod.
Thank you
Dok
Edit : I just realised I probably didn't word the end very well. Sorry for any inconvenience