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> ();
}
}
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.
Your answer
Follow this Question
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