Change variable on another script

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”

public var animator:Animator;
public var speed:float;
 
function Start(){
animator=GetComponent(Animator);
}
function Update () {
animator.speed = 1;
}

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)

function OnTriggerEnter () {
    GameObject.Destroy (gameObject) ;
         }

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.

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

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. :confused:

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 :smile:

So in my script that would result in:

public static var animator:Animator;
    public static var speed:float;
     
    function Start(){
    animator=GetComponent(Animator);
    }
    function Update () {
    animator.speed = 1;
    }
 function OnTriggerEnter () {
 		AnimatorVar.animator.speed = 2;
 		
                   //GameObject.Destroy (gameObject) ;
             }

Or am i still missing something? Can’t get it to work with this.
Regards.

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 :slight_smile:

Can’t get it to work sadly. :S
Appreciate your help man.

Isn’t GetComponent supposed to be written like GetComponent<>()…?

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.