Search Unity

How I can get public variables from a gameObject?

Discussion in 'Scripting' started by blaze, Aug 5, 2012.

  1. blaze

    blaze

    Joined:
    Dec 21, 2011
    Posts:
    211
    I have two objects. A "jumpTrigger" and a "characterController".
    In the characterController script, I have the boolean "Jump".
    Then, when the character pass in the trigger, I want to set to TRUE the Jump boolean.
    How I can do that?
     
  2. sevisin

    sevisin

    Joined:
    Dec 26, 2010
    Posts:
    145
  3. blaze

    blaze

    Joined:
    Dec 21, 2011
    Posts:
    211
    @sevisin
    Yes, look what I have now:
    Code (csharp):
    1. //get Jump
    2.     void OnTriggerEnter(Collider t)
    3.     {
    4.         if (t.CompareTag("Bot"))
    5.         {
    6.             CharacterController controller = t.GetComponent<CharacterController>();
    7.             float vel = controller.velocity.x;
    8.  
    9.             if (vel < 0)
    10.             {
    11.                 Debug.Log("Esquerda");
    12.             }
    13.             else
    14.             {
    15.                 Debug.Log("Direita");
    16.             }
    17.            
    18.         }
    19.     }
    20.  
    Instead of show a Debug Message, I want to set to true the Jump Boolean in the character controller.
     
    Last edited: Aug 5, 2012
  4. sevisin

    sevisin

    Joined:
    Dec 26, 2010
    Posts:
    145
    @blaze
    Sorry, I misread your original post.
    If you're working on triggers, you want to look up OnTriggerEnter(). That can modify your variable.

    It would look something like this (Unityscript)

    Script1.js
    Code (csharp):
    1.  
    2. var activateJump : boolean = false;
    3.  
    4. function Update()
    5. {
    6.    if (activateJump)
    7.    {
    8.       ActivateJump();
    9.    }
    10. }
    11.  
    12. function ActivateJump()
    13. {
    14.    // jump code
    15. }
    16.  
    17. // when gameObject hits a trigger...
    18. function OnTriggerEnter(hit : Collider)
    19. {
    20.    activateJump = true;
    21. }
    22.  
    if the trigger is the code you want to jump, you would just write something like this...

    Script1.js
    this script is attached to the trigger object
    Code (csharp):
    1.  
    2. var activateJump : boolean = false;
    3.  
    4. function Update()
    5. {
    6.    if (activateJump)
    7.    {
    8.       ActivateJump();
    9.    }
    10. }
    11.  
    12. function ActivateJump()
    13. {
    14.    // jump code
    15. }
    16.  
    Script2.js
    code on gameObject
    Code (csharp):
    1.  
    2. function OnTriggerEnter(hit : Collider)
    3. {
    4.    var tempScript : Script1; // notice that Script1 is the name of the script on the trigger object
    5.    tempScript = hit.gameObject.transform.GetComponent("Script1");
    6.    tempScript.activateJump = true;
    7. }
    8.  
    All of the code above was written on the fly and untested.

    Hope that helps,

    -S
     
  5. Apprentice

    Apprentice

    Joined:
    Feb 9, 2012
    Posts:
    74
    answer: controller.setjump(true); could do the trick but would require you to have the function void setjump(bool check){jump=check}; inside the character controller class.

    BUT /!\ This wont work like you think it would. Setting the Boolean wont cause the action to happen. So first you need to checkout how to use get and set methods. Once done you need to script something like a movement action which does coordinate directional movements as you basically would do when you controlled it via W,A,S,D.

    The character controller does have the functions for the movements and handles the collision, but you need a script that handles the sequences of the movements through accessing the character controller scripts functions.

    Check the 2D Platformer tutorial from the unity page for its 2d character controller class.
     
  6. blaze

    blaze

    Joined:
    Dec 21, 2011
    Posts:
    211
    @sesivin
    Thanks, now I understood!

    @Apprentice
    Look this code, this is working for me:
    Code (csharp):
    1. public class JumpTrigger : MonoBehaviour {
    2.  
    3.     public bool right;
    4.     public bool left;
    5.     public Player script;
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15.  
    16.     //get Jump
    17.     void OnTriggerEnter(Collider t)
    18.     {
    19.         if (t.CompareTag("Bot"))
    20.         {
    21.             CharacterController controller = t.GetComponent<CharacterController>();
    22.             float vel = controller.velocity.x;
    23.             script = t.GetComponent("Player") as Player;
    24.  
    25.             if (vel < 0  left)
    26.             {
    27.                 script.GoJump();
    28.             }
    29.             if (vel > 0  right)
    30.             {
    31.                 script.GoJump();
    32.             }
    33.            
    34.         }
    35.     }
    36. }
    Inside my FSM, in the patrolState, I check if the character.IsGrounded and if Jump is true, if yes, he jumps.
    I dont konw if it is the best for performance, but is working!

    I will put some texture to show to what side the jumpTrigger is configured, then I post here the scene.

    Thanks!
     
  7. Apprentice

    Apprentice

    Joined:
    Feb 9, 2012
    Posts:
    74
    Don't bother about performance during your first cycles on that topic. The made up controller sure(most cases) does use up more performance than one you would do custom for your needs, but is less complex to use and you probably will not even mention any issues.

    And remove the empty Update() from your script. an empty Update() is still called every frame.