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 GregoryNeal · Apr 25, 2015 at 09:48 AM · scripting problemnullreferenceexception

NullReferenceException when getting value from different script

Hi guys, I have an issue with NullReferenceException, I just can't figure it out. I have 2 scripts on different objects, WorldOrder and SpawnGround. SpawnGround is supposed to get the world order from WorldOrder and then spawn the ground around the player. Here's SpawnGround, the error line is in OnTriggerEnter2D and marked with a comment:

 public class SpawnGroundFront : MonoBehaviour {
     public GameObject player;
 
     int groundIndex;
     int loadDist;
     List<string> worldOrder;
 
     void Start(){
         player = GameObject.FindWithTag("Player");
         try{
             loadDist = player.GetComponent<WorldOrder>().loadDist;
             groundIndex = player.GetComponent<WorldOrder>().groundIndex;
             worldOrder = player.GetComponent<WorldOrder>().worldOrder;
         }
         catch(NullReferenceException ex){
             Debug.Log("Player not found!");
         }
     }
 
     void OnTriggerEnter2D(Collider2D other){
         if(player != null){
             if(other.gameObject == player){
                 //get the stack of world names
                 float vel = other.GetComponent<Rigidbody2D>().velocity.x;
 
                 //player is moving forward
                 if(vel > 0){
                     //there is no ground data to load
                     if(worldOrder[groundIndex + loadDist + 1] == null){ //THIS LINE GIVES THE ERROR
                         SpawnForwardNew();
                         PushBackwardStack();
                         //groundIndex++;
                     }
                     //there is some ground data to load
                     else{
                         SpawnForwardLoad();
                     }
                 }
                 else if(vel < 0){
                     PushForwardStack();
                 }
             }
         }
         else{
             Debug.Log("No player object!");
         }
     }


Here is WorldOrder:

 public class WorldOrder : MonoBehaviour {
 
     //other methods treat this as a stack where push adds a value to the front or back, same with pop
     public List<string> worldOrder;
 
     public GameObject[] groundObj;
     //public GameObject[] platformObj;
 
     public int loadDist = 2;
     public int groundIndex;
 
     void Awake(){
         if(GameObject.FindWithTag("Ground") == null){
             SpawnInitial();
             groundIndex = loadDist;
         }
     }
 
     void GetWorldOrder(){
         GameObject curr = GameObject.FindWithTag("Ground");
     }
 

Comment
Add comment
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

2 Replies

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

Answer by GregoryNeal · Apr 25, 2015 at 10:37 AM

The object that holds WorldOrder is a child of my player so I should have used GetComponentInChildren instead of GetComponent.

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
1

Answer by Tomer-Barkan · Apr 25, 2015 at 10:32 AM

Seems like you never assign a value to worldOrder in the WorldOrder class, so it's null. Then you try to acess one of it's members.

Try changing the line to

 public List<string> worldOrder = new List<string>();

The only other thing I can think about is that OnTriggerEnter2D is called before Start(), though I can't think of why this happens. You can check this by adding logs in each method and see which one is called first.

Comment
Add comment · Show 2 · 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 GregoryNeal · Apr 25, 2015 at 10:36 AM 0
Share

Hey man thanks for the reply, I did figure it out just now though. The object that holds WorldOrder is a child of my player so I should have used GetComponentInChildren ins$$anonymous$$d of GetComponent.

avatar image Tomer-Barkan · Apr 25, 2015 at 10:39 AM 0
Share

Thats weird, I thought of that scenario but then:

 worldOrder = player.GetComponent<WorldOrder>().worldOrder;

would throw an exception if the GetComponent returns null and you try to access the worldOrder variable...

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

20 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

Related Questions

Random: NullReferenceException: Object reference not set to an instance of an object 2 Answers

ScriptableObject reference resetted after a method is finished. 2 Answers

HELP!!! This NullReferenceException won't go away! :( 1 Answer

How can I access another script in my prefab to set a variable after my network player setup runs? 1 Answer

Trigger reference problem 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