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 tsx5000 · Jan 16, 2014 at 11:56 AM · variablesaccessing from any script

Error in accessing variables from another script

I have two prefabs each having its own individual script. I am then loading both prefabs through a third script and need to work between their variables. Here is what I have done :

 public class GameManager : MonoBehaviour {
 
     public Transform buttonPrefab;
     public Transform playerPrefab;
 
     private GameObject b;
     private GameObject p;
     private ButtonScript button;
     private PlayerController player;
 
     // Use this for initialization
     void Start ()
     {
         p = Instantiate (playerPrefab, new Vector3 (0, 0, 0), Quaternion.identity) as GameObject;
         b = Instantiate (buttonPrefab) as GameObject;
 
         button = b.gameObject.GetComponent<ButtonScript> ();
         player = p.gameObject.GetComponent<PlayerController> ();
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (button.IsClicked)
                         player.Move ();
     }
 }

But I keep getting the Error: NullReferenceException: Object reference not set to an instance of an object.

What am I doing wrong ?

EDIT: After restarting Unity the problem was gone. Thanks anyway!

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 Dugular7 · Jan 16, 2014 at 12:13 PM 0
Share

Which line does the NullReferenceException point to?

avatar image tsx5000 · Jan 16, 2014 at 12:25 PM 0
Share

it points to : if (button.IsClicked)

avatar image ankush_Kushwaha · Jan 16, 2014 at 12:52 PM 0
Share

what is IsClicked . is it your public variable ?

avatar image Sir-Irk · Jan 16, 2014 at 01:16 PM 0
Share

Yeah it would help to know about the script it's accessing. Have you tried adding if(button != null)?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JoaoOliveira · Jan 16, 2014 at 12:01 PM

I think instantiating GameObjects from a Transform reference won't work.

Try changing these lines:

 public Transform buttonPrefab;
 public Transform playerPrefab;

to:

 public GameObject buttonPrefab;
 public GameObject playerPrefab;
Comment
Add comment · Show 11 · 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 tsx5000 · Jan 16, 2014 at 12:06 PM 0
Share

nope, that doesn't fix the problem. Any other suggestions?

avatar image JoaoOliveira · Jan 16, 2014 at 12:08 PM 0
Share

In which line do you get the NullReferenceException?

Also, there is no need to write "b.gameObject.GetComponent...". 'b' is already a reference to the GameObject. (This not the problem, just pointing it out)

avatar image mohanrao164 · Jan 16, 2014 at 12:10 PM 0
Share

did both prefab contains those scripts

avatar image tsx5000 · Jan 16, 2014 at 12:23 PM 0
Share

@JoaoOliveira : I am getting the error in the Update function and I tried doing "button.gameObject.GetComponent ();" and it was still throwing an error hence the unnecessary modification.

@mohanrao164: yup both prefabs have been assigned their respective scripts

avatar image JoaoOliveira · Jan 16, 2014 at 12:27 PM 0
Share

Yes it changes nothing, there is just no need to use 'gameObject' in this situation.

If you are getting NullReferenceException in update, then it must be because one of the prefabs doesn't contain the correct script. Did you hit "Apply" after attaching the script to the game objects?

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

accesing variable from other script 4 Answers

Get access and Flip between different variables from GameManager 0 Answers

More elegant use of struct and class 1 Answer

How to get damage variable from another script ? 1 Answer

Accessing variables from other scripts 0 Answers


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