Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 stereosound · Sep 17, 2011 at 08:05 PM · javascriptcoroutinefunctionscriptingbasics

Unityscript: Accessing functions from other scripts

Say you're looking to do something like this:

TEST_SCRIPT.js attached to empty object: function Start() { StartCoroutine("CoStart"); }

 function CoStart() : IEnumerator
 {
     while (true)
         yield CoUpdate();
 }
 
 function CoUpdate() : IEnumerator
 {
 if (Input.GetKeyDown("space")) {
     StartCoroutine(TEST_SCRIPT2.MOVEME);
     }
 }

TEST_SCRIPT2.js attached to another empty object: var MOVEME : GameObject;

 function MoveCube() : IEnumerator
 {
 MOVEME = GameObject.Find("Cube");
 MOVEME.transform.position = Vector3 (0,0,10);
 }

What's the syntax to actually call the Coroutine, as StartCoroutine(TEST_SCRIPT2.MoveCube); will get the error: Assets/TEST_SCRIPT.js(16,37): BCE0020: An instance of type 'TEST_SCRIPT2' is required to access non static member 'MoveCube'.

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 Peter G · Sep 17, 2011 at 08:47 PM

The error tells you everything wrong with your code. Coroutines can only be called on object instances. So you need an instance of TEST_SCRIPT2 to call your function MoveCube(). Something like this would work:

    var testObj : TEST_SCRIPT2 = FindObjectOfType(TEST_SCRIPT2);
    //and normally you start the coroutine on the object like this.
    testObj.StartCoroutine( MoveCube() );
Comment
Add comment · Show 11 · 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 stereosound · Sep 17, 2011 at 09:09 PM 0
Share

Doesn't seem to work I'm afraid. Could be because testObj comes up as null if you print(testObj); (could this be because the scripts aren't on the same object)?

avatar image Peter G · Sep 17, 2011 at 09:26 PM 0
Share

That means you don't have an instance of your script. Put it on some GameObject.

avatar image stereosound · Sep 18, 2011 at 06:20 AM 0
Share

They are definitely attached to an object. As I said, they're both attached to empty gameobjects. testObj.StartCoroutine( $$anonymous$$OVE$$anonymous$$E() ); doesn't work in either case, I tried referencing it ins$$anonymous$$d as ("$$anonymous$$OVE$$anonymous$$E") which didn't produce an error but because of the problem with testObj I can't verify that it works.

Again, the testObj I don't think works because there is no object of type TEST_SCRIPT2; you are in reality trying to find an object with a component of TEST_SCRIPT2. But I could be off base here.

avatar image ShinAli · Sep 18, 2011 at 06:58 AM 0
Share

I would think FindObjectOfType returns null because TEST_SCRIPT2 is not an object by Unity's definition, but a component. I would use the FindObjectOfType() function to search for the game object with the script, then call the GetComponent method of the returned object.

$$anonymous$$eep in $$anonymous$$d that the FindObjectOfType() function would be kind of slow, depending on the scene. You may have to use singleton pattern so you can have access right away to the game object in question.

avatar image stereosound · Sep 18, 2011 at 07:05 AM 0
Share

So theoretically, say I have two separate gameobjects (obj1 and obj2) with scripts TEST_SCRIPT and TEST_SCRIPT2 attached, respectively. I want to call function Run$$anonymous$$e() in TEST_SCRIPT2 from TEST_SCRIPT1, how would I do this? Assu$$anonymous$$g I have obj2 in a gameobject var, yadda yadda.

$$anonymous$$y problem is it seems to me I can't use StartCoroutine() to call a function in a different script because the functions themselves aren't static (aka, it has nothing to do with what's inside of them, because I make no reference to the objects they're attached to).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

running code outside of update! 3 Answers

My function isn't ending but is being repeatedly being called from the update function 1 Answer

Coroutine a function within a loop? 1 Answer

JS and C# how to open in different programs. 1 Answer

how to make object stay destroyed after OnTriggerEnter? 1 Answer


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