UI Button Swap Objects
Hi, For my first project on Unity I'm trying to implement a UI Button that swap between different object model. I've found some old coding used for cycling objects and tried to use it on my own but i can't quite understand what i'm missing. Here is a copy of the script i wrote using System.Collections;using System.Collections.Generic; using UnityEngine;
public partial class Activate : MonoBehaviour {
public static bool viewCube;
public static bool viewCylinder;
public static bool viewSphere;
private int itemSelector;
private int objectCount;
`void Start() {
itemSelector = 1;
}
void Update() {
Debug.Log(itemSelector);
Debug.Log(viewCube);
Debug.Log(viewCylinder);
Debug.Log(viewSphere);
ViewableObjects = FindObjectOfType(typeof(scriptaray)) as scriptaray[];
objectCount = new ViewableObjects;
if (Input.GetButtonDown)
{= "Button") }
{
itemSelector -= 1;
}
if (itemSelector >= 4)
{
itemSelector = 1;
}
if (itemSelector <= 0)
{
itemSelector = 3;
}
if (itemSelector == 1)
{
viewCube = true;
}
else {
viewCube = false;
}
if (itemSelector == 2)
{
viewCylinder = true;
}
else {
viewCylinder = false;
}
if (itemSelector == 3) {
viewSphere = true; }
else {
viewSphere = false; }
for (int = 0; int < objectCount; int++)
{
if (viewCube) {
if (ViewableObjects.gameObject.tag == "cube")
{
ViewableObjects.gameObject.setActive.enabled = true;
}
else {
ViewableObjects.gameObject.render.enabled = false;
}
}
if (viewCylinder) {
if (ViewableObjects.gameObject.renderer.tag == "Cylinder")
{
ViewableObjects.gameObject.renderer.enabled = true;
}
else {
ViewableObjects.gameObject.renderer.enabled = false;
}
}
if (viewSphere) {
if (ViewableObjects.gameObject.renderer.tag == "Sphere")
{
ViewableObjects.gameObject.renderer.enabled = true;
}
else {
ViewableObjects.gameObject.renderer.enabled = false;
}
}
}
}
} `
I really appreciate any help you can provide.
You might want to edit your question so all of the script renders correctly. It makes it more difficult to read when it's formatted like this. There's a button you can use to get a script to format correctly.
Also, please give any hints you can about what is actually going wrong. If the class is partial for a real reason, like you define the rest of it somewhere else, please include that.
Answer by MaxGuernseyIII · Sep 16, 2017 at 12:15 AM
There are a lot of things wrong with this script.
In this line, you attempt to assign an object of type ViewableObjects to a variable of type int. You also don't construct your instance of ViewableObjects correctly.
objectCount = new ViewableObjects;
This bit just won't parse at all:
if (Input.GetButtonDown)
{= "Button") }
{
There are a lot of basic C# errors. That is, problems that would be problems if you were just writing an algorithm in Visual Studio; they aren't Unity specific, near as I can tell. You might want to go use a tool like HackerRank to help walk you through the language and get you past these kinds of problems.