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 Monko · Jul 01, 2013 at 07:38 PM · functioncall

How to call a function in another script

I have 2 scripts (Javascript, by the way), scriptA and scriptB. By searching in unity's answers forum, I learned how to call a function in scriptA that is inside scriptB. Like this:

ScriptA

 var scriptB: ScriptB;
 scriptB.Test();

ScriptB

 function Test() {
 //code
 }

This code works fine, but i need a to call a function with inputs and outputs. For example, if I use this for ScriptA and ScriptB, I get an error:

ScriptA

 var scriptB: ScriptB;
 print(scriptB.Test(2,3));

ScriptB

 function Test(a : int,b : int):int {
 return a*b
 }

I don't know what the problem is, and I can't find any help on google, unity community, or anywhere else. Can someone show me how to call a function from another script with inputs and outputs?

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

3 Replies

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

Answer by Monko · Jul 02, 2013 at 04:48 PM

After lots more searching, I did finally come up with a solution!!!!!

The variable scriptB wasn't never defined, so I decided to do this:

Script A:

 var scriptB: ScriptB;
 var voxelCube  : GameObject;
 voxelCube = GameObject.Find("VoxelCube");//this is the name of the object
                                          //that contains script B`
 scriptB= voxelCube.GetComponent(Scr_World);

 print(scriptB.Test(2,3));

Script B:

 function Test (a : int, b : int)
 {
    print (a*b);
 }

This makes it possible to call a function located in Script B from Script A, including the inputs and outputs of the function.

Comment
Add comment · 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
1

Answer by Jessy · Jul 01, 2013 at 07:55 PM

You should have posted your error, but regardless of whether this solves your problem, don't name your variable the same as the class. The compiler is probably telling you that Test isn't a static method. Capital letters are not used for fields. Some people will tell you that you have the freedom to do that, but it shows no respect for other coders. It ought to be disallowed by the compiler.

Comment
Add comment · Show 2 · 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 Monko · Jul 01, 2013 at 09:10 PM 0
Share

Understood. I edited my code above. Also, here is the error message it gives me:

Object reference not set to an instance of an object.

avatar image Kiloblargh · Jul 02, 2013 at 04:39 PM 0
Share

Look at ScriptA in the inspector. You see where it says scriptB None (ScriptB) ? That is what the error message means. You need to drag the object with ScriptB on it to that slot in the inspector; or else have Script A find and assign it in Awake().

avatar image
-1
Wiki

Answer by create3dgames · Jul 01, 2013 at 07:54 PM

Maybe something like this:

ScriptA

 function Start ()
 {
     BroadcastMessage( "Test", 2, 3);
 }

ScriptB

 function Test (a : int, b : int)
 {
    print (a*b);
 }
Comment
Add comment · Show 5 · 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 Monko · Jul 01, 2013 at 09:13 PM 0
Share

I tried it, and it doesn't work. Also, I don't even know what Broadcast $$anonymous$$essage does.

Anyone else have any suggestions?

avatar image Jessy · Jul 02, 2013 at 02:23 PM 1
Share

Look up one of the thousands of pages that explain why you get "Object reference not set to an instance of an object."

avatar image Kiloblargh · Jul 02, 2013 at 04:46 PM 0
Share

It's solved, see above. The reason Jessy was getting snippy with you is that this has nothing to do with your particular problem, and nothing to do with the title of your question; it's just that you don't know how to assign references in the inspector, which is one of the first things you need to know to use Unity; which is why they put it in the first section of the manual.

avatar image Loius · Jul 02, 2013 at 04:50 PM 1
Share

Your problem is not that you have inputs/outputs, it's that you have a null reference. scriptB is equal to 'null'; you have to set it to something before you can use it.

avatar image create3dgames · Jul 02, 2013 at 08:31 PM 0
Share

@$$anonymous$$onko What do you mean, you tried it, and it "doesn't work". I've never heard such a thing. Can you be a little more descriptive? Did you get any errors? And if you don't know what Broadcast$$anonymous$$essage is then look it up. Trust me, it works perfectly.

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling a Function from other script ( C# ) 1 Answer

How to call a function from another script without referencing it? 1 Answer

Call GUITexture and script 1 Answer

How to reference another script and call a function in C# ? 2 Answers

Call a Function from another Script 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