Call a component with different parameters?

Hey, this is my first time using unity and I’m very happy with the power it has.

I’m just not very good in JavaScript nor on C# nor on Boo, but I do program in C++ and Ruby. I just have a question about at least one of those languages…

How do you call components or scripts in Js, C# or Boo?
In C++ or C, you can call a function or a script like “MyFunction(parameter1, parameter2, parameter 3);”
I have already seen this page: http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html
but I can’t manage to understand how to tell the script what parameters to change.

I’m making a 2d platformer game in Unity and it’s like 3d world constrained to 2d movement and pure 2d player (textured plane) to simulate sprite.

And I want to make the sprite to change if I press the left/right arrow or press A key to jump or D to do a dash. I have this script:

And I want to call it changing uvAnimationTileX, uvAnimationTileY and framesPerSecond, also changing “_MainTex” to something else:

renderer.material.SetTextureOffset (“_MainTex”, offset);
renderer.material.SetTextureScale (“_MainTex”, size);

Is this possible?

This is two questions (how to call component functions, how to animate texture tiling). Could you split the second question off please?

2 Answers

2

It is just like C++. Get a reference to the target object and call a function or set a property on it:

target.DoStuff(1,2,3);
target.myProp = 123;

To get the reference (the target variable above), use GetComponent as you have noted:

target = targetGameObject.GetComponent.<TheTargetScript>();

For best performance, keep the reference rather than finding it again every frame.

To get the GameObject containing the component, you can, for example, use GameObject.Find.

Alternatively, you can set the references from the Inspector:

var target : TheTargetScript; // drag target to this in inspector

Thank you!
But there weren’t two questions… I was just asking for the component call… I already knew how to animate by tiling. :S