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 vmadman · Aug 20, 2012 at 09:00 AM · game objectexternalscope

Accessing external scripts (yet again)

Yes, I recognize that there are 50 answers to this question, but I've read everything I can find and its just not registering with me.

If someone could just help me with this maybe I can move on.

I started here, but it seems to be riddled with either pseudo-code or its not giving me enough information to actually understand what they're talking about. They use the term "otherScript" too frequently.

So, my specific example, should be very easy. Forgive me if I give too much info, I just wanna be sure we're on the same page.

Here's what I've got:

  1. I created a game object in my scene, its named "_globalScripts"

  2. It has a script attached to it named "animationManager.cs"

  3. animationManager.cs has a class named animationManager : MonoBehavior

  4. I have another script, "behaviorController.js" (javascript) attached to a vehicle

  5. Within behavior controller I want to call animationManager.sayHello(), which does a quick Debug.Log

The reason I have a mixture of C# and JS is because I'm trying to learn to make the transition. My newer stuff is going to be C#.

Please tell me what I should add to behaviorController's members (like, private var), what I should add to Start() to attach the reference, and what I type in Update() to call sayHello().

I know its just my ignorance, but the docs seem to suggest that I can create a private variable of type 'animationManager' (as implied by OtherScript).. but I get unrecognized type errors on that. I've also managed to get a reference to animationController through GameObject.find("_globalScripts").GetComponent("animationManager") .. but without proper typecasting I cannot execute sayHello().

Subquestion: Is it possible to skip adding anything to Start() and just call my reference gathering stuff directly in the JS class members, i.e: private var animationManager : something = GameObject.find(....)...?

Thanks for your help.

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

2 Replies

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

Answer by Khada · Aug 20, 2012 at 10:23 AM

Here is an example, you will have to alter it to your needs (I'll use the class/object names from your question):

 //----------------------------------------------------------
 //animationManager.cs
 //----------------------------------------------------------
 
 //make sure this exists in the class and is public
 public void sayHello()
 {
     Debug.Log("hello!");
 }
 
 
 //----------------------------------------------------------
 //behaviorController.cs        (using cs as i don't use js)
 //----------------------------------------------------------
 
 //this will be our reference to the animation manager class
 animationManager kAnimMan = null;
 
 void Start()
 {
     //get the global manager object
     GameObject kGloMan = GameObject.Find("_globalScripts");
 
     //get the animation component from it and store it
     kAnimMan = kGloMan.GetComponent<animationManager>();
 }
 
 void Update()
 {
     //will tell the animation manager to say hello every frame
     kAnimMan.sayHello();
 }

Note: I wrote that off the top of my head, I can't promise it's 100% perfect.

EDIT: To answer your sub question, no (though you could use a field).

Comment
Add comment · Show 8 · 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 vmadman · Aug 20, 2012 at 01:44 PM 0
Share

I appreciate the response. Your explanation falls exactly in line with my understanding. The JS adaptation of what you put there is almost identical. I put "private var kAnim$$anonymous$$an : animation$$anonymous$$anager" in the code and I get an error "The name 'animation$$anonymous$$anager' does not denote a valid type ('not found'). Did you mean UnityEditor.AnimationClip..."

I am doing something wrong in exposing that animation$$anonymous$$anager class, it fails even before I can sayHello().. but both the animation$$anonymous$$anager class and sayHello are public.

avatar image Khada · Aug 20, 2012 at 01:55 PM 0
Share

Ahh no, it was a typo, animation$$anonymous$$anager in the Update func should be kAnim$$anonymous$$an. I'll edit it.

avatar image vmadman · Aug 20, 2012 at 02:05 PM 0
Share

$$anonymous$$y script fails on the declaration of kAnim$$anonymous$$an as type 'animation$$anonymous$$anager', long before Update() is even called. Unity is not seeing animation$$anonymous$$anager as a valid type. I did not copy+paste your code, I already calling "kAnim$$anonymous$$an.sayHello()", so the typo wasn't the cause of my troubles. Thanks :)

avatar image Khada · Aug 20, 2012 at 02:11 PM 0
Share

To check: You have a class called animation$$anonymous$$anager right (according to your original question you do)?

avatar image Khada · Aug 20, 2012 at 05:06 PM 0
Share

Did that do the trick? Don't forget to mark the question as answered if it did.

Show more comments
avatar image
0

Answer by fafase · Aug 20, 2012 at 09:02 AM

Have you seen this one? http://answers.unity3d.com/questions/246211/having-scripts-interact.html

It is only UnityScript but the idea goes the same with C#.

Try placing the C# script in the standard asset folder and keep the Js outside.

Scripts inside the std asset are compiled before others.

Comment
Add comment · Show 3 · 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 vmadman · Aug 20, 2012 at 01:51 PM 0
Share

Thank you for the response. $$anonymous$$y error happens at "var other : ScriptName;", or in my case "var kAnim$$anonymous$$an : animation$$anonymous$$anager;" -- I get the error "The name 'animation$$anonymous$$anager' does not denote a valid type ('not found'). Did you mean..."

Every answer I find is the same, I can't figure out what I'm doing wrong in exposing animation$$anonymous$$anager as a type.

avatar image fafase · Aug 20, 2012 at 05:31 PM 0
Share

your issue might be the "" around the component name:

 GameObject.Find("_globalScripts").GetComponent(animation$$anonymous$$anager);

try and remove. I have seen cases here where the "" would create error or null. And it is Find not find, but that oculd just be a wrong copy paste.

avatar image vmadman · Aug 20, 2012 at 06:49 PM 0
Share

I've tried that as well. I am actually failing on the declaration of the class member, before Start() or Update() ever run.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Turn Mesh Render On In Another Script? 2 Answers

Boolean while looking at a game object? 1 Answer

How can I access other scripts and their functions? 3 Answers

How do I access a gameobjects variables in a seperate script? 3 Answers

Destroying game object next to player 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