Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Clarinet · Jul 19, 2012 at 12:56 AM · functioncall

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Clarinet · Jul 19, 2012 at 02:41 AM 0
Share

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?

avatar image Seth-Bergman · Jul 19, 2012 at 02:44 AM 0
Share

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.

avatar image Clarinet · Jul 19, 2012 at 03:15 AM 0
Share

**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.

avatar image Clarinet · Jul 19, 2012 at 04:02 AM 0
Share

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?

avatar image Seth-Bergman · Jul 19, 2012 at 04:07 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges