- Home /
[C#] How to make this code toggleable
using UnityEngine;
using System.Collections;
public class ResizeToggle: MonoBehaviour
{
private GameObject ObjectToScale = null;
private float ScaleIncrement = 0.5f;
void OnGUI()
{
if
{
ObjectToScale.transform.localScale = new Vector3((transform.localScale.x + ScaleIncrement), (ObjectToScale.transform.localScale.y + ScaleIncrement), (ObjectToScale.transform.localScale.z + ScaleIncrement));
}
else
{
ObjectToScale.transform.localScale = new Vector3((ObjectToScale.transform.localScale.x - ScaleIncrement), (ObjectToScale.transform.localScale.y - ScaleIncrement), (ObjectToScale.transform.localScale.z - ScaleIncrement));
}
}
}
I have this so far, but I don't know how to complete it. I don't know how to make it toggle-able.
can you be more specific as to how you want to toggle it?
toggle between adding the ScaleIncrement and subtracting it. So basically a toggle to resize a game object. The toggle will be an UI toggle button.
you can also use the same formula for both cases where you multiply the scaleIncrement with -1 when the button is pressed
Answer by tinglers · Mar 12, 2017 at 03:03 PM
https://docs.unity3d.com/ScriptReference/GUI.Button.html may be what you are looking for here. otherwise i'd suggest either having 2 buttons to increment and assign them functions or handle it all in a monobehaviour script using the OnMouseDown() and InputGetKey();
I don't want 2 buttons, I just want one toggle button to handle it.
Answer by RGS-Aaron · Mar 13, 2017 at 10:02 AM
Well your if statement needs a condition. The condition needs to return a boolean value(true or false). This is a very basic programming skill, I suggest you learn the basics of programming before continuing.
Do you have a reason for putting your code in OnGUI?
To answer your question anyway. You need a boolean variable, that gets changed based on some input. for example when a button is pressed. "if(Input.GetButton("MyButton")) myBool = !myBool;" and then use the myBool for the condition of the if statement.
Answer by c4ssh3rn · Mar 16, 2017 at 10:06 AM
public GameObject ObjectToScale = null;
public float ScaleIncrement = 0.5f;
public float WidthScaleIncrement = 0.8f;
public Vector3 offsetOnScale = Vector3.zero;
public enum State {INIT, NOT_SCALED, SCALED, WIDTH_SCALED }
public State state;
public override void Toggle()
{
if (state == State.INIT)
{
//ObjectToScale.transform.localScale = new Vector3((transform.localScale.x + ScaleIncrement), (ObjectToScale.transform.localScale.y + ScaleIncrement), (ObjectToScale.transform.localScale.z + ScaleIncrement));
ObjectToScale.transform.localScale = ObjectToScale.transform.localScale + new Vector3(ScaleIncrement, ScaleIncrement, ScaleIncrement);
ObjectToScale.transform.localPosition = ObjectToScale.transform.localPosition + offsetOnScale;
state = State.SCALED;
}
else if(state == State.SCALED)
{
ObjectToScale.transform.localScale = new Vector3((ObjectToScale.transform.localScale.x - ScaleIncrement), (ObjectToScale.transform.localScale.y - ScaleIncrement), (ObjectToScale.transform.localScale.z - ScaleIncrement));
state = State.NOT_SCALED;
}
else if(state == State.NOT_SCALED)
{
ObjectToScale.transform.localScale = new Vector3((ObjectToScale.transform.localScale.x + WidthScaleIncrement), ObjectToScale.transform.localScale.y, ObjectToScale.transform.localScale.z);
state = State.WIDTH_SCALED;
} else
{
ObjectToScale.transform.localScale = new Vector3((transform.localScale.x - WidthScaleIncrement), ObjectToScale.transform.localScale.y, ObjectToScale.transform.localScale.z);
state = State.INIT;
}
}
any tips on how to improve?
this seems alright. you could use a switch case to avoid all the if-else statements, this gets especially more important if you have alot of these cases. if you're not going to expand this ever then i say don't bother. I see you have 4 states but only 3 if statements. Generally it's a good rule of thumb to have an if statement for all cases you are testing.