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 humbled · Nov 07, 2012 at 08:06 PM · variableget component

How to access a variable from another class (GetComponent()?)

 Hi.

     I am new to Unity and my programming skills are very rusty. Please help me with my Unity3D problem.

 I have 2 classes.
 public class Ball: MonoBehaviour
 ()
 public class Star: MonoBehaviour
 ()

 Ball playerBall; //is the main player game object in the Unity Hierarchy
 Star itemStar; //is a star item game object in the Unity Hierarchy // the playerBall collects stars within the game world.

 My question is how can I access a variable from another class.

 int starCount;// is a variable inside of the Ball script

 I want to access the variable starCount from inside the Star script.

 //The following OnTriggerEnter(Collider collision) is located inside the Star script
 void OnTriggerEnter(Collider collision)
 {
 if (collision.gameObject.tag == "ballTriggerCollision")
 {
 Ball tempBall;
 tempBall= this.GetComponent("Ball") as Ball;
 tempBall.GetComponent<Ball>().starCount += 1; // ****** THIS IS THE LINE THAT CREATES THE ERROR

 killStar();

 }
 }

 The game runs fine until the exact same moment that the Ball Trigger collider collides with the star.
 I get an error.

 NullReferenceException: Object reference not set to an instance of an object
 Ball.OnTriggerEnter (UnityEngine.Collider collision) (at Assets/Scripts/Ball.cs:97)
 UnityEditor.Toolbar:OnGUI()

 tempBall.GetComponent<Ball>().starCount += 1; // ****** THIS IS THE LINE THAT CREATES THE ERROR


 I don't want to add the Ball game object as a variable on the Star script because then I will need to rewrite much of my game code.

 If you can help me then I thank you for your help in advance. Your time and energy is appreciated. 
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 Fattie · Nov 07, 2012 at 08:08 PM 0
Share

massive explanation on unityGE$$anonymous$$S.com

would be pointless to copy it here

avatar image humbled · Nov 11, 2012 at 09:56 AM 0
Share

Thank you for the resources you have provided. The website http://unityGE$$anonymous$$S.com is greatly helpful. Your time and your help is appreciated!

1 Reply

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

Answer by FakeBerenger · Nov 07, 2012 at 11:14 PM

The error comes from that line :

 tempBall= this.GetComponent("Ball") as Ball;

As you can see, you're getting the component from "this", which is the star. What you want instead, is to get the component from the ball's gameObject. So, do this instead :

 tempBall = collision.gameObject.GetComponent<Ball>();
 tempBall.starCount += 1; // You could test if tempBall is != null, unless you're sure it won't be.
Comment
Add comment · Show 1 · 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 humbled · Nov 11, 2012 at 10:05 AM 0
Share

Thank you for your answer. The Console produces no more errors and the game is increasing the starCount! Your time and your help is appreciated!

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

10 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

Related Questions

can i change a variable from other js? 1 Answer

Passing a Script Name to a Function 2 Answers

Weapon script 0 Answers

Why the variable updates only twice? 1 Answer

How to delete a Variable in a script in game 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