Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by c4ssh3rn · Mar 10, 2017 at 07:03 AM · c#scripting problemscripting beginnerscriptable object

[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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tinglers · Mar 12, 2017 at 02:45 PM 0
Share

can you be more specific as to how you want to toggle it?

avatar image c4ssh3rn tinglers · Mar 12, 2017 at 02:47 PM 0
Share

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.

avatar image tinglers c4ssh3rn · Mar 12, 2017 at 05:08 PM 0
Share

you can also use the same formula for both cases where you multiply the scaleIncrement with -1 when the button is pressed

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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();

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image c4ssh3rn · Mar 14, 2017 at 01:20 PM 0
Share

I don't want 2 buttons, I just want one toggle button to handle it.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tinglers · Mar 16, 2017 at 10:21 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how do i add a offset to my sword rotation? 2 Answers

Enabling multiple Monobehaviour Components in a game object 1 Answer

I have a rotation script that is applied to two different objects. If I rotate one it also rotates the other and I do not want that. 2 Answers

how to use right stc on gamepad like Input.mousePosition 0 Answers

How would I access my list from another function? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges