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 /
This question was closed Apr 10, 2013 at 02:04 AM by Loius for the following reason:

Duplicate Question

avatar image
-1
Question by JackBid · Apr 09, 2013 at 12:21 PM · nullreferenceexceptionfunctioninstance

How to call a function from another script.

Script A: I have one script, which is attached to the main camera and contains lots of GUI information.

Script B: I have another script which is attached to a prefab which gets instantiated many times throughout the game, But is not present at the beginning of the game, it get instantiated later by the user.

I need to call a function from script B in script A.

in Script B:

 public function Test(){
 print("it worked");
 }

in Script A:

 var ScriptB : ScriptB;
 
 ScriptB.Test():

This gives me a NullReferenceException, "NullReferenceException: Object reference not set to an instance of an object"

Comment
Add comment · Show 5
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 liamcary · Apr 09, 2013 at 12:31 PM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html

http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

avatar image Olgo · Apr 09, 2013 at 12:36 PM 0
Share

Sounds like it would be checking for it before it exists. You're seeing this error during runtime right? I would put an extra check before ScriptB.Test(); to see if a prefab exists first.

avatar image JackBid · Apr 09, 2013 at 01:17 PM 0
Share

I tried adding a variable in for an extra check and I got the same message. Also yes it is happening at runtime.

 var testNum = 5;
 public function Test(){
 print("it worked");
 }
 

 var ScriptB : ScriptB;
 
 print(ScriptB.testNum);
 ScriptB.Test():
avatar image Benproductions1 · Apr 10, 2013 at 01:56 AM 0
Share

@Other$$anonymous$$ods Can you recall how may times this question has been asked before?

Here are the duplicate Questions I got from 1 google search:

http://answers.unity3d.com/questions/13248/how-to-call-a-function-from-another-script-getting.html

http://answers.unity3d.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html

http://forum.unity3d.com/threads/57072-Calling-function-from-other-scripts-c

http://answers.unity3d.com/questions/11206/call-a-function-in-a-different-script-using-javasc.html

http://answers.unity3d.com/questions/337750/why-cant-i-call-function-from-another-script.html

http://answers.unity3d.com/questions/261891/better-way-to-call-function-from-another-script-fr.html

http://answers.unity3d.com/questions/265270/call-function-in-another-script.html

http://answers.unity3d.com/questions/185288/calling-a-function-from-another-script.html

http://answers.unity3d.com/questions/200095/calling-functions-from-another-script-not-attached.html

http://answers.unity3d.com/questions/8770/accessing-functions-in-other-scripts.html

http://answers.unity3d.com/questions/197065/access-functions-in-another-script.html

http://answers.unity3d.com/questions/423161/calling-functions-from-another-script.html

http://answers.unity3d.com/questions/130931/calling-functions-from-any-script-.html

http://answers.unity3d.com/questions/393220/calling-function-from-other-scripts.html

http://answers.unity3d.com/questions/238794/simply-calling-a-method-from-another-script-.html

http://answers.unity3d.com/questions/128805/how-do-i-call-methods-from-other-scripts-using-c.html

How did you not find all these yoursefl? Why did you not do a google search? Why did you post this question?

avatar image Loius · Apr 10, 2013 at 02:04 AM 0
Share

they're also all over the suggested questions list, which pops up as you type your question and is on the right of the screen now where i can see it.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Function is Null. It's not possible. why. 1 Answer

NullReferenceException 0 Answers

Dictionary Function not responding to Key Calls 0 Answers

NullReferenceException: Object reference not set to an instance of an object CannonFire.Update 0 Answers

NullReferenceException help 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