Question by
mrhctran · Oct 27, 2015 at 09:28 AM ·
guiactivatedeactivate
Deactivate GameObject and Image on NGUI button
I'm a novice programmer and need some advice. I got my current code working correctly, but I have noticed a problem. Since my code is attached to a Gui button, I got my gameobject and images to activate / deactivate correctly, however, if I pressed on another NGUI button, the gameobject doesn't disappear when I select a different button. I also noticed the images remain hidden when I go return to the subject matter. I know i can deactivate the gameobject on the Onclick, but I think there got to be a better and way of activating and deactivating gameobject and images. I have attached an image on the bottom. Any suggestions?
#pragma strict
var images : Texture2D[];
var i : int = 0;
var showImage = true;
public var objectToHide:GameObject;
public var hideBeforeShow:boolean=true;
function Start(){
if(hideBeforeShow == true){
objectToHide.SetActive(false);
}
}
function OnGUI () {
if (showImage)
GUI.DrawTexture(Rect(365,20,1500,900),images[i]);
if (GUI.Button(new Rect(880,930,100,50),"PREVIOUS"))
{
if (i > 0)
i--;
GUI.DrawTexture(Rect(200,50,100,100), images[i]);
print(i);
showImage = true;
objectToHide.SetActive(false);
}
if (GUI.Button(new Rect(1000,930,100,50),"NEXT"))
{
if (i < images.Length - 1)
i++;
GUI.DrawTexture(Rect(200,50,100,100), images[i]);
print(i);
showImage = true;
objectToHide.SetActive(false);
}
if (GUI.Button(new Rect(1120,930,100,50),"3D MODEL"))
{
showImage = false;
objectToHide.SetActive(true);
}
}
[1]: /storage/temp/57027-screenshot.jpg
screenshot.jpg
(89.0 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612042833im_/https://answers.unity.com/themes/thub/images/avi.jpg)