Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Pyrgos · Dec 08, 2017 at 05:07 AM · childrenforeach

Foreach and GetComponentInChildren, getting NullReferenceException

EDIT: after sleeping over this, still no solution and I at this point I think I am probably unlearning what I have so far. I have assigned public components to make sure they're all there and that the game is not missing any. I check it in the inspector. It shows all the public components. All components match what I am expecting. And yet, when I assign a local variable in the script, I just get the null reference all the same. I truly don't get what is going on... :'(

I updated the code as it is now.

First post: Ok, after 3 hours of trying to get a solution, I call it a quit. After so many iterations, I can't even remember what I have tried or not. I have searched for a solution on the internet unsuccessfully. I keep getting a null reference on the below code.

All the code seems to be running well besides the fact that I can't get the script component in the instantiated object child - which is triggered through a OnTriggerEnter code - everything looks good on the inspector.

What is it I'm doing wrong?

 //Instantiates the movement area, and this triggers a lot of colliders throughout, public instances assigned, everything showing up in the inspector. 
         movementArea = Instantiate (movementArea, gm.pawn.transform.position, gm.pawn.transform.rotation);
         MovementTileScript[] capsules;
         capsules = movementArea.GetComponentsInChildren<MovementTileScript> ();
 
         foreach (MovementTileScript capsule in capsules)
         {
             tile = capsule.movementScript.tileScript; // first ERROR occurs RIGHT HERE
             pawn = capsule.movementScript.pawnStats;
 
             if (tile  == null)  //check if there is a tile, first of all. TODO if there isn't, contemplate destroying the capsule.
             {
                 Debug.Log ("Does not exist");
                 continue;
             }
 
             tile.MovementAllowed = true;
 
             if (pawn != null)
             {
                 Debug.Log ("I have to reach this 10 times");
                 if (gm.playerTurn != pawn.playerOwner) // if player is not the owner of the pawn, it can be attacked, and movement can be made here
                 {
                     pawn.CanBeAttacked = true;
                     //tile.MovementAllowed = true;
                     Debug.Log ("I should not have reached this");
                 } 
                 else 
                 {
                     tile.MovementAllowed = false; // if player is the same as the pawn, he simply cannot land a pawn on top of another
                 }
             }
 
         }



Here is the remaining code in regards to the collision trigger:

 public class MovementTileScript : MonoBehaviour {
 
     public GameObject pawn;
     public PawnStats pawnStats;
     public GameObject tile;
     public TileScript tileScript;
     public MovementTileScript movementScript;
 
     private GameManager gm;
 
     void OnTriggerEnter(Collider other)
     {
         gm = GameObject.FindGameObjectWithTag ("GameController").GetComponent<GameManager> ();
         movementScript = GetComponent<MovementTileScript> ();
         //tileScript = other.GetComponent<TileScript> ();
         //pawn = other.GetComponent<PawnStats> ();
 
         if (other.tag == "Tile") 
         {
             this.transform.parent = other.transform;
             tile = other.gameObject;
             tileScript = other.GetComponent<TileScript>();
             tileScript.MovementAllowed = true;
         }
         if (other.tag == "Pawn") 
         {
             other.transform.parent = this.transform;
             pawn = other.gameObject;
             pawnStats = other.GetComponent<PawnStats> ();
         }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pyrgos · Dec 08, 2017 at 03:07 PM

Ok, after testing a little more, I have realised that, right after the Instantiation, it is the foreach that occurs, even before the OnTriggerEnter.

I was taking the logics of VBA, which has no problem interrupting a Sub when an event happens, but I am guessing that in C#, a class will run the full code on the very frame, and only then it runs the rest. Correct me if I am wrong.

This will have me thinking about alternative solutions.

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

117 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 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 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 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 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

Looking through all the children doesn't work 1 Answer

Enabling and disabling child particles 2 Answers

Issue where Fading out Game Object causes screen to blank and then reload with gameobject gone 0 Answers

I'm trying to acess a child of the childs of an object for using them. 0 Answers

Scaling Challenge Help Needed! 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