Search Unity

[need help] How to Change and work with the Variables in Runtime with Unity GUI ?!

Discussion in 'Immediate Mode GUI (IMGUI)' started by Extreme_u3Damon, May 25, 2013.

  1. Extreme_u3Damon

    Extreme_u3Damon

    Joined:
    Aug 6, 2012
    Posts:
    66
    Hey Guys , i have started a project which is like SIM content (About Heavy Vehicles like TANK , TRUCK ,...) now if e.g. want to Change the Mass of my Tank , or Max Speed , other things in Runtime by the help of GUI , what should i do ?!

    could any one guide me as simple as possible ?
     
  2. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    - you should attach a new script to your GUI (anywhere in the hierarchy).
    - you should create public fields that will use as referenced to your Tank (you'll drag&drop the Tank game object to the inspector slot in the editor)
    - your script will have the OnGUI() method
    - inside you will draw a slider
    - when your slider changes the a value, you'll propagate this change to your Tank

    Something like:

    Code (csharp):
    1. var hSliderValue:float = 0.0;
    2. var tank:Tank; // reference to tank
    3.  
    4. function OnGUI () {
    5.     var newValue = GUI.HorizontalSlider (Rect (25, 25, 100, 30), hSliderValue, 0.0, 10.0);
    6.     if (hSliderValue != newValue) {
    7.         hSliderValue = newValue;
    8.         if (null == tank)
    9.             throw new Exception("Tank not set");           
    10.         // set whatever value
    11.         tank.transform.x = hSliderValue;
    12.     }
    13. }
     
  3. Extreme_u3Damon

    Extreme_u3Damon

    Joined:
    Aug 6, 2012
    Posts:
    66
    Thanks so much ! i examine the way you suggest , and now have this question , if i want to change the mass of the TANK (i attached a RigidBody Component to it) .... then how am i gonna make the slider do the work for me ?
     
  4. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    There are many ways, but at this point (with the knowledge you have) the simplest way is to make a public variable for the object that you want to tweak, so you can drop it to that slot (like the Tank in the previous example).

    Then, in code, you target the mass (using the your_object.gameObject.rigidbody.mass)

    GameObject.rigidbody
    Rigidbody.mass
     
  5. Extreme_u3Damon

    Extreme_u3Damon

    Joined:
    Aug 6, 2012
    Posts:
    66
    Thank you so much :)
    well , can we say after we doing this e.g
    var Mass = Tank.gameObject.rigidbody.mass = newValue

    (newValue is pointing to what you write before for me :) )

    also are you familiar with Play Maker ? some one told me i can do it in no time in PlayMaker ,i learn it watch several tuts and ... i tried but , its not responding - or i dont just know how it works !
     
  6. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Look, there are plenty of video tutorials on creating games with scripting, as well as on Playmaker.

    You cannot expect people to solve your problems without learning it all by yourself. :)