• 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 pesetskyps · Feb 06, 2015 at 07:56 AM · variablescript errorexception

Other script function call with variables

Hello, I have class A with prefab populated from inspector. I'd like to call it from class B. I call it like below and got NullReference exception for Class's A prefabButton being null. How can I can the method from another class using destination class properties populated via inspector.

 public class A: MonoBehaviour {
     public GameObject prefabButton;
     public void FunctionA()
     {
       GameObject newButton = Instantiate(prefabButton) as GameObject;
     }
 }
 
 public class B: MonoBehaviour
 {
     public void FunctionB()
     {
         A populator = new A();
         populator.FunctionA(items);
     }
 }

Comment
Add comment · Show 2
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 Jiro-Ng · Feb 06, 2015 at 08:13 AM 0
Share

Which function you are trying to call from which classes? And this class A and class B script, are they attach on a same GameObject?

avatar image pesetskyps · Feb 06, 2015 at 08:44 AM 0
Share

Edited the origin question code. I try to call A's method.

1 Reply

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

Answer by sysameca · Feb 06, 2015 at 09:01 AM

It will be null, because you didn't assigned no game object to prefabButton. Your problem is that you are trying to create a MonoBehaviour with the new keyword instead of getting it as component. MonoBehaviour must be added to a game object and then the instance of the script is retreived through GetComponent().

  1. Create a new object in the hierarchy name it ObjectA and add the A component to it.

  2. Drag and drop your game object which you want to populate over the prefabButton inspector field.

  3. Create a new game object in the hierarchy and add component B to it.

  4. In class B's method FunctionB() instead of creating "new A()" replace it with: GameObject.Find("ObjectA").GetComponent().FunctionA();

Comment
Add comment · Show 7 · 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 pesetskyps · Feb 06, 2015 at 09:07 AM 0
Share

Thanks, but this approach makes hard to apply software design patterns from "standard" program$$anonymous$$g that I knew before. :) Can you bring the light on this?

avatar image sysameca · Feb 06, 2015 at 09:17 AM 0
Share

Unity is entity-component based game engine. A lot of OOP practices can't be exactly achieved the same way you do in standard program$$anonymous$$g. It has it's pros i't has it's cons. I can explain for hours, but there are some other questions related to this so i will give you this link to start with. $$anonymous$$aybe the link is not 100% what you are looking for, but I am sure you can google the rest: How does unity entity system works

avatar image pesetskyps · Feb 06, 2015 at 09:19 AM 0
Share

The direction to read is exactly what I want, many thanks

avatar image sysameca · Feb 06, 2015 at 09:20 AM 0
Share

http://docs.unity3d.com/$$anonymous$$anual/UsingComponents.html

http://docs.unity3d.com/$$anonymous$$anual/TheGameObject-ComponentRelationship.html

avatar image Bonfire-Boy · Feb 06, 2015 at 09:32 AM 1
Share

@pesetskyps You can instantiate gameobjects in code, but you don't do it by calling a constructor. Classes derived from $$anonymous$$onobehaviour shouldn't have constructors, you don't create instances of them directly. Ins$$anonymous$$d you create a gameobject and attach components. The basic idiom is..

 GameObject go = new GameObject();
 go.AddComponent<A>();
 go.GetComponent<A>.initialisationFunction(...); // optional - if you need a function to set up the "A" component like a constructor would 

 
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I am trying to access a variable from another script. 1 Answer

Script is causing crashes and i Don't know what to do. 2 Answers

Modifying variable from another script (error) 1 Answer

Instantianig a teleporter prefabs with destination variables 1 Answer

On Click, increment variable 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