- Home /
How do I make different gameobjects appear, every time you click the GUI.button?
To make my question more clear, I want it so that when you click the button in the middle of the screen, a picture comes up, then every time you click it a different picture comes up,at a random location on the screen. I'm clueless of where to go about this, any ideas?
Answer by Stormizin · Nov 08, 2013 at 07:44 PM
You just need something like that bro:
public Texture2D picture1;
void OnGUI(){
if(GUI.Button(new rect(50,50,50,50), "Click me!")){
GUI.DrawTexture(new rect(Random.Range(min,max), Random.Range(min,max), 50, 50), picture1);
}
}
Note: $$anonymous$$ and max is the integer number you want randomize. $$anonymous$$g: $$anonymous$$ -> 1 max -> 10 it will randomize from 1 to 10.
I wonder if when he said "then every time you click it", he meant the picture or the button.
I ment the button. The more times you click it the more random images comes up
Answer by tw1st3d · Nov 08, 2013 at 07:52 PM
I feel like this would work, however it hasn't been tested.
using UnityEngine;
using System.Collections;
public class Swap : MonoBehavior
{
public GameObject[] objects;
private int currentObject = 0;
private Random random = new Random();
private void Start()
{
for(GameObject i in objects)
{
Instantiate(i, new Vector(0,0,0), i.transform.rotation);
i.SetActive(false);
}
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
currentObject = random.Next(0, objects.Length - 1);
objects[currentObject].SetActive(true);
}
}
}
$$anonymous$$ind of forgot to add in disabling any of the currently enabled objects on key press, so you'll have to add that. Just use the same for() loop.
I'm not experienced in C#, so I wouldn't know how to fix it since it gives a ton of errors, but thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613121458im_/https://answers.unity.com/themes/thub/images/avi.jpg)