using UnityEngine;
using System.Collections;
public class Swap : MonoBehaviour {
public GameObject objectCube;
public GameObject objectSphere;
public GameObject objectTest;
void OnGUI() {
if (GUI.Button (new Rect(550,275,100,40),"Prev!")) {
objectCube.SetActive (true);
objectSphere.SetActive (false);
objectTest.SetActive (false);
}
if (GUI.Button (new Rect(650,275,100,40),"Next!")) {
objectCube.SetActive (false);
objectSphere.SetActive (true);
objectTest.SetActive (false);
}
}
}
The two bottons work fine with the Cube and Sphere model, but I dont know how to add the third model" Test " to the bottons, I mean when you push next so you activate the first model, and deactivate the others, and when you push Prev! botton so you activate the second model, but how to creat the botton that changes between all 3 models?
Anyone who could help?
You’ll need a couple more things to get that. First you need an array or list (or whatever container, helpful link below) to hold all the things you want to cycle through. Then you need to have an int var “pointer” which says which one you are currently pointing at.
You can then set the gameobjects in the container at “pointer position + 1” or “pointer position -1” for next/prev to active and deactivate the gameObject at the “pointer position”. You’ll need to be careful about going off the ends of the container though, so a little checking needed there.
you could then write a function to make only the object you want active to be active
function SetActiveObject(index : int)
{
for(var i : int= 0; i<objectArray.length;i++)
{
if(i==index)
objectArray[i].setActive(true);
else
objectArray[i].setActive(false);
}
}
then you just need to keep track of the active object index, add for next subtract for previous and clamp the value of it between 0 and the length of the array.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public GameObject[] Objects;
private int _index;
void OnGUI() {
if (GUI.Button(new Rect(10, 10, 80, 20), "Prev")) {
_index --;
if (_index <0)
_index = Objects.length;
ProcessActive();
}
if (GUI.Button(new Rect(100, 10, 80, 20), "Next")) {
_index ++;
if (_index >= Objects.length)
_index = 0;
ProcessActive();
}
}
private void ProcessActive() {
for (int i = 0; i < Objects.length; i++) {
Objects[i].SetActive(i == _index);
}
}
}
Thats my code now and I still get errors. and the error says: Assets/NewBehaviourScript.cs(21,30): error CS1061: Type UnityEngine.GameObject[ ]' does not contain a definition for length’ and no extension method length' of type UnityEngine.GameObject[ ]’ could be found .
You are right I must learn the basics, but I am not really a game developer, I am working with 3D Graphic and rendrings, and this is a small school project with some few simple scripts.
Now I dont have any compile errors, and when I set array to two elements and add the models in the scene to each element, and when I push the " Prev " and " Next " bottons, nothing happens, still have both objects on the scene…
Now this is the compile error I get " Assets/NewBehaviourScript.cs(38,80): error CS0103: The name `i’ does not exist in the current context"
And another thing, when I run the game, both object are automaticaly are in the scene, Then when I push Next or Prev then it deactivates one of them. Is there any way to solve this, that when I run the game, just an empty scene or one of the objects are activated and not all of them.
There is one small issue left though, When you press the Prev botton, first it takes you to a totally empty scene instead of getting you to the second object, is there any fix to remove that empty scene, or jump over it?