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
1
Question by Shankar · May 06, 2013 at 11:28 AM · objectmonobehaviour

How to call a function from one class to another class?

Rad.cs:

 public class Rad: MonoBehaviour
 {
 void show()
 {
 print("show in Rad");
 }
 }

Soud.cs:

  public class Soud: MonoBehaviour
 {
 public Rad rad;
 void Start()
 {
 rad=new Rad();
 }
 void Update()
 {
 rad.show();
 }
 } 

i want to call a function(show()) from Rad class in Soud class. But i got NullReferenceException: Object reference not set to an instance of an object.

Thanks

Shankar

Comment
Add comment · Show 4
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 Fornoreason1000 · May 06, 2013 at 12:08 PM 0
Share

void Show isn't public for one( it needs to be public, since your referencing it). also Unity doesn't like you making $$anonymous$$onobehaviour's or anything that inherits it with the "new" keyword, which you have done.

avatar image navadeep2011 · May 06, 2013 at 12:17 PM 0
Share

k that's fine, then what is the way to use?

avatar image Fornoreason1000 · May 06, 2013 at 12:22 PM 0
Share

with mono behaviors you need to assign them via inspector or Instantiate them. the null reference is probably being caused by $$anonymous$$ono saying "no you can't do this"

avatar image Shankar · May 06, 2013 at 12:31 PM 0
Share

Thank you $$anonymous$$r @Fornoreason1000

Can u please help me how should i instantiate that from the other class.

2 Replies

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

Answer by Fornoreason1000 · May 06, 2013 at 12:45 PM

ok I'm guessing your new. anything that is or inherits from a mono behavior CANNOT be created with the new keyword. Anything else can such as Vectors. you need to assign RAD from the inspector... if your creating RAD on run time use a prefab and instantiate that. keep in mind that you need an instance to instantiate you can get one from the assets folder, assigning one to a var, or getting one from the scene. for your script just assign the RAD object in the inspector.

be aware you can also just look for a an existing instance of an object

As for the error, null reference means you referencing something that is null. unlike C++ null isn't equal to 0

e.g x = null

So: x + 2 = undefined.

the reason your getting null is by using the new keyword to initialize the monobehaviour, this gets disallowed and thus never really happens. leaving RAD null making referencing Show impossible;

     using UnityEngine;
 using System.Collections;
 
 public class RAD : MonoBehaviour {
 
     public void Show()
     {
         Debug.Log("Showing Rad, this script is working!");
 
     }
 }


next script:

 using UnityEngine;
 using System.Collections;
 
 public class SOUD : MonoBehaviour {
 
     public RAD rad; //you must assign this
     // Use this for initialization
     void Start () {
 
         //one way to inititlize a monobehaviour
        // rad = FindObjectOfType(typeof(RAD)) as RAD; 
         
     }
     
     // Update is called once per frame
     void Update () {
         rad.Show();
     }
 }

http://msdn.microsoft.com/en-us/library/yzh058ae.aspx http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html

if your not making an object out of RAD and going to use it globally and never use an instance for it you can make it static. but be very careful when using static. with static classes and methods you can't inherit from monobehaviour or attaching to game objects. but you can definitely reference them inside scripts. I suggest reading up on statics and researching them before using them since it can cause problems with GC(Garbage collection)

http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/98f28cdx(v=vs.71).aspx

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
2

Answer by The-Oddler · May 06, 2013 at 01:39 PM

You seem new, so I'll try to be as clear as possible. Though I would suggest you do some "my first unity game" tutorials, they should get you going in no time.

Now, if you make a new Monobehaviour script it means it's a component. If a script is a component you can add it to an object. This is very powerful, but requires a little getting used to if you've never done this kind of programming. If at this point you have no clue what I'm talking about, you should really do some beginner tutorials first.

Now, if you want to talk to a component from within an other one. All you have to do is make a public variable (like you did: public Rad rad;) And then in the editor drag the object with that component onto the other object's public field (it'll be visible in the inspector.)

alt text

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

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

find out if your object is being Occluded 0 Answers

How to return true if object is in invisible object? 1 Answer

Coding A Tornado Particle Effect 0 Answers

What is the reason for this NullReferenceException? 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