Object Reference not Set
I'm having an issue with my script not finding a gameobjects position, and I believe it's how I'm setting it but I'm not quite sure how to do it properly. First, here are my two declarations. Please note that the players variable is set using GetAllGameObjectsWithTag("Players")
GameObject[] players;
public Vector3[] _pos;
...
for (int x = 0; x < players.Length; x++)
{
players[x].GetComponent<Animator>().SetBool("MovingLeft", false);
players[x].GetComponent<Animator>().SetBool("MovingRight", false);
_pos[x] = players[x].gameObject.transform.position;
}
This first two lines that set the bool work just fine, so I've got the right game objects, but no matter how I set the _pos[x], it always errors saying it can't find it. How do I set the position to the corresponding game object?(Also, I really only need the 'x' value of position though either way is fine)
By looking at your code right now players and _pos arrays must have same number of indexes so this code can work. For example:
If there are 5 players and there are 3 positions you will get error because _pos[4] is null. You have only 3 positions.
_pos[0] = players[0]..gameObject.transform.position; O$$anonymous$$
_pos[1] = players[1]..gameObject.transform.position; O$$anonymous$$
_pos[2] = players[2]..gameObject.transform.position; O$$anonymous$$
_pos[3] = players[3]..gameObject.transform.position; ERROR
_pos[4] = players[4]..gameObject.transform.position; ERROR
@$$anonymous$$arshal_Alessi
_pos[x] = players[x].gameObject.transform.position;
but players is already an array for GameObjects.
so this should do ins$$anonymous$$d
_pos[x] = players[x].transform.position;
I think the problem is that you haven't defined how many elements can go into your array. It is usually best to use Lists if you are working with an undefined capacity for a collection as Lists have dynamic capacity. Here is a workaround though, but you still need
using System.Collections.Generic;
List<Vector3> positions = new List<Vector3>();
foreach(GameObject player in players)
{
player.GetComponent<Animator>().SetBool("$$anonymous$$ovingLeft", false);
player.GetComponent<Animator>().SetBool("$$anonymous$$ovingRight", false);
positions.Add (player.transform.position);
}
_pos = positions.ToArray ();
This works because we are assigning _pos with a finite amount of Vector3s, it uses a List.ToArray() method which returns an array for you which can be assigned to another array. $$anonymous$$uch like how you can assign
players = GameObject.FindGameObjectsWithTag("Player");
Answer by Marceta · Nov 18, 2016 at 10:39 AM
If I understood you correctly you want to collect positions of all players. You could do it like this:
GameObject[] players;
public List<Vector3> _pos = new List<Vector3>();
for (int x = 0; x < players.Length; x++)
{
players[x].GetComponent<Animator>().SetBool("MovingLeft", false);
players[x].GetComponent<Animator>().SetBool("MovingRight", false);
_pos.Add(players[x].gameObject.transform.position);
}
This worked, but I'm still not sure why $$anonymous$$e did not. You are correct that they are corresponding arrays, yet $$anonymous$$e failed to find the player with the position. Do you have any idea as to why that might have happened?
$$anonymous$$ake those two variables public (this will show them on inspector) and check in play mode are they same length (same number of items). They are probably not same length that's why you are getting error. Because if you are trying to access _pos[5] and _pos have only 3 items you will get error because _pos[5] doesn't exist and it's null. So you are trying to set value to something that doesn't exist.
Your answer
Follow this Question
Related Questions
Vector3.Lerp not working properly, making the player bounce around 2 Answers
Make object move back and forth 2 Answers
Need help with this code? thanks. 2 Answers
Rotation set to face direction of movement but doesn't always work 1 Answer
i need to change box position in y axis if i hit from down (c# script) 2d 1 Answer