Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Change variable on another script

Discussion in 'Scripting' started by 10111110, May 25, 2014.

  1. 10111110

    10111110

    Joined:
    May 25, 2014
    Posts:
    5
    Hi everyone. I'm fairly new to Unity3D - actually programming in general.
    I'm trying to do a basic game with PowerUps and a few animations.

    But there is a already the first problem, I can't get the PowerUps to do what i want them to.

    What i planis that when the player collides with the PowerUp (BoxCollider) the players Animator Speed should increase for a limited time (10sec or so).

    I wrote a script to change the Animator Speed in a variable (float) and it works.
    But I am having trouble changing that variable, through the script attached to the PowerUp when the collision happens.

    Already tried a lot of things, but nothing seems to work for me. I hope someone can help me out.

    The script that manipulates the Animator Speed called "AnimatorVar" attached to "Player"
    Code (csharp):
    1.  
    2. public var animator:Animator;
    3. public var speed:float;
    4.  
    5. function Start(){
    6. animator=GetComponent(Animator);
    7. }
    8. function Update () {
    9. animator.speed = 1;
    10. }
    11.  
    This is the Script i did for the PowerUp attached to "Trigger" (basicly only a test for the collider itself with no functionality other than destroyng the box after the collison)
    Code (csharp):
    1.  
    2. function OnTriggerEnter () {
    3.     GameObject.Destroy (gameObject) ;
    4.          }
    5.  

    So what i need is basically a way to change the variable from = 1 to for example = 2 and give it a timer of 10 secs.

    Best Regards.
     
    Last edited: May 25, 2014
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    To change a variable on another script you have a few choices. One less choice, and my preferred method, which is exclusive to C#, so you using Java can not. One reason I suggest switching languages.

    In C# you can have a script as a variable. Now, with that variable, you have access to that script from this script.

    Having said that, GetComponent, or SendMessage().
     
  3. 10111110

    10111110

    Joined:
    May 25, 2014
    Posts:
    5
    First of all thank you for your reply. Using C# is not really an option for me, as i have no knowledge in using it and i don't want to mix languages here.

    I did try the GetComponent Method but just can't get it to work. :/
     
  4. peterdeghaim

    peterdeghaim

    Joined:
    Apr 10, 2014
    Posts:
    154
    Make it a static and then access it by going CLASSNAME.VARIABLENAME

    To make it a static type in static after public. E.g. public static var

    When accessing it in your script do NAMEOFCLASS.NAMEOFVAR . E.g. scoreClass.score = 1;

    Goodluck :D
     
  5. 10111110

    10111110

    Joined:
    May 25, 2014
    Posts:
    5
    So in my script that would result in:

    Code (csharp):
    1. public static var animator:Animator;
    2.     public static var speed:float;
    3.      
    4.     function Start(){
    5.     animator=GetComponent(Animator);
    6.     }
    7.     function Update () {
    8.     animator.speed = 1;
    9.     }

    Code (csharp):
    1.  function OnTriggerEnter () {
    2.         AnimatorVar.animator.speed = 2;
    3.        
    4.                    //GameObject.Destroy (gameObject) ;
    5.              }
    Or am i still missing something? Can't get it to work with this.
    Regards.
     
    Last edited: May 25, 2014
  6. peterdeghaim

    peterdeghaim

    Joined:
    Apr 10, 2014
    Posts:
    154
    I'm no pro with the animators but shouldn't AnimatorVar.animator.speed = speed?
    Set speed to 1 and try again.

    And on your second script make it YOURCLASSNAMETHATWASONSCRIPTONE.speed = 2

    Hope that helps :)
     
  7. 10111110

    10111110

    Joined:
    May 25, 2014
    Posts:
    5
    Can't get it to work sadly. :S
    Appreciate your help man.
     
  8. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    Isn't GetComponent supposed to be written like GetComponent<>()...?
     
  9. 10111110

    10111110

    Joined:
    May 25, 2014
    Posts:
    5
    Thanks to everybody that contributed to this. I have managed to get what i wanted by making the player the one triggering the collision and attaching a script to him.