- Home /
Perform function on another Game Object
Hey all, I was wondering if it was at all possible to call a function on another object than the one the script is actually attached to. So if so, it might look something like this:
Function OnTriggerEnter (hit : Collider) {
Destroy(gameObject);
// & perform a different function on another game object
}
What I'm trying to do here is destroy one game object and activate the "moving platform" script that Unity provided on a different object. So if there's a way, I'd like to know it! Thanks!
Answer by Seth-Bergman · Jul 19, 2012 at 02:19 AM
like this:
var movingPlatform : PlatformScript;
Function OnTriggerEnter (hit : Collider) {
movingPlatform.StartMoving();
Destroy(gameObject);
}
Note that Destroy(gameObject) must be the last line, since the object running the script is then destroyed)
EDIT: to be clear, PlatformScript would be the name of the script which contains the function you wish to access, StartMoving the name of the function. You would have to set the variable to the object as well. The easiest way is to drag and drop the moving platform object (which must contain the script PlatformScript) onto the variable "movingPlatform" in the inspector.
SECOND EDIT: instead of calling a function, you could call a var which tells the platform it can move, like:
var movingPlatform : PlatformScript;
Function OnTriggerEnter (hit : Collider) {
movingPlatform.canMove = true;
Destroy(gameObject);
}
then the movement code on the platform would be in the update function:
var canMove = false;
function Update(){
if(canMove){
//movement code
//edit: code based on script in comment below
transform.position=Vector3.Lerp(transform.position,ToThat.transform.position,Time.deltaTime * speed);
}
}
then it's all up to the movement logic...
Okay, that seems a little better. However, whenever the Collider is hit, the platform appears at a random place on its course, paused. I have the Start$$anonymous$$oving function on WaitForFixedUpdate, because if it was just on FixedUpdate, the platform moves automatically without a cue. Am I doing something wrong?
The problem is the Start$$anonymous$$oving function, but I need more info. Post the script!
note: the function itself is only being called once, as the trigger object containing the trigger script is then destroyed. So the Start$$anonymous$$oving function would need to be something that keeps going. I'll edit my answer above, take a look.
**Here is the script I used attached to the cue object:
var movingPlatform : $$anonymous$$ovingCube;
function OnTriggerEnter (hit : Collider) { movingPlatform.WaitForFixedUpdate(); Destroy(gameObject); }
**& here is the script that's attached to the moving object:
var FromThis : GameObject; var ToThat : GameObject;
var speed : float = 0.1;
function FixedUpdate () { var weight = $$anonymous$$athf.Cos(Time.time speed 2 $$anonymous$$athf.PI) 0.5 + 0.5; transform.position = FromThis.transform.position weight + ToThat.transform.position (1-weight); }
I think your edited answer might work, I'll try it out.
This is going smoothly! Thanks for your help so far! There's just one problem left: Once the cue object is touched, the platform still appears in a random part of its course, but it does move along the course as it's supposed to. Is there a way to start it up from where it's at before it's activated?
ok, just caught something else.. if you WERE calling a function, it would not be nested in the FixedUpdate or any other function:
function FixedUpdate(){
//...
}
function Start$$anonymous$$oving(){
//..
}
unlike FixedUpdate, which is called automatically every fixed frame, Start$$anonymous$$oving() would only be called by an explicit call. Your movement code could be fixed too. To move smoothly from one point to another, try:
transform.position = Vector3.Lerp(transform.position, ToThat.transform.position,Time.deltaTime * speed);
in place of your last line. The final code would look like:
function Update(){
if(can$$anonymous$$ove)
transform.position = Vector3.Lerp(transform.position, ToThat.transform.position,Time.deltaTime * speed);
}
p.s. I believe using "Time.deltaTime", as above, achieves the same effect as using FixedUpdate rather than Update, not sure if it really makes a difference which way you do it though
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Rpc client issue 0 Answers
Call a function at a certain frame in animation without Animation Events? 1 Answer
{ ERROR } call a function from other script 1 Answer
How to reference another script and call a function in C# ? 2 Answers