Int returns 0 after value is assigned, ,Int returns 0 within certain method, Can't figure out why.
I have an int called "index" which is within a script "Body". I assign index in another script when I Instantiate the GameObject it's on. No where else is the value of index changed anywhere though whenever called through a certain method the value always returns 0. I even checked to make sure the value was assigned properly by checking the inspector and calling the value through update by Debug, but any time the value is called through the method it returns 0.
The first script is the "Player" Script which is where I created the new GameObject which has the "Body" script and where "index" is assigned. Index is assigned in the Method "AddBodyPart()"
public GameObject head;
public GameObject bodyPrefab;
public GameObject waypointPrefab;
GameObject lastWaypointPos;
List<GameObject> waypoints = new List<GameObject>();
public List<GameObject> bodyParts = new List<GameObject>();
[SerializeField]int startAmount = 3;
public float speed = 5;
float xDir;
float yDir;
bool blockW = true, blockA, blockS, blockD = false; //Block the inversed direction so you dont go backwards
void Start()
{
lastWaypointPos = null;
blockW = true;
//Starts the Snake moving up
xDir = 0;
yDir = 1;
CreateBody();
}
void Update()
{
ForceMovement();
if(MovementInput())
{ CreateWaypoint(transform.position); }
}
void ForceMovement()
{
//Forces Forward
transform.position += new Vector3(xDir * Time.deltaTime * speed, yDir * Time.deltaTime * speed);
}
private bool MovementInput()
{
//Movement
if (Input.GetKeyDown(KeyCode.W) && !blockW)
{ xDir = 0; yDir = 1; blockS = true; blockA = false; blockD = false; return true; }
else if (Input.GetKeyDown(KeyCode.A) && !blockA)
{ xDir = -1; yDir = 0; blockD = true; blockW = false; blockS = false; return true; }
else if (Input.GetKeyDown(KeyCode.S) && !blockS)
{ xDir = 0; yDir = -1; blockW = true; blockA = false; blockD = false; return true; }
else if (Input.GetKeyDown(KeyCode.D) && !blockD)
{ xDir = 1; yDir = 0; blockA = true; blockW = false; blockS = false; return true; }
else
{ return false; }
}
void CreateWaypoint(Vector2 positon)
{
GameObject waypoint = Instantiate(waypointPrefab, new Vector2(positon.x, positon.y), Quaternion.identity) as GameObject;
lastWaypointPos = waypoint;
AddNewWaypointToBodies(waypoint);
}
void CreateBody()
{
for (int i = 0; i < startAmount; i++)
{
AddBodyPart(i == 0);
bodyParts[i].transform.position -= (Vector3.up * (i + 1)) / 2;
if(i == 0) { bodyParts[0].GetComponent<Body>().targetFollow = head; } //Sets target path to Head
}
}
//
// Assigned In the Method Below
// VVVVVVVVVVVVVVVVVVVVVVVVV
void AddBodyPart(bool firstBody)
{
GameObject body = Instantiate(bodyPrefab, transform.parent) as GameObject;
bodyParts.Add(body);
body.GetComponent<Body>().SetIndex(bodyParts.Count - 1);
body.name = "body " + bodyParts.Count;
if(!firstBody)
{
body.GetComponent<Body>().waypoints = bodyParts[bodyParts.Count - 2].GetComponent<Body>().waypoints;
if (lastWaypointPos == null) {
body.GetComponent<Body>().targetFollow = bodyParts[bodyParts.Count - 2];
}
else
{
body.GetComponent<Body>().AddWaypoint(lastWaypointPos);
}
}
}
void AddNewWaypointToBodies(GameObject waypoint)
{
for(int i = 0; i < bodyParts.Count; i++)
{
bodyParts[i].GetComponent<Body>().AddWaypoint(waypoint);
}
}
In the "Body" Script I never change its value though when I call it in the Method "SetNextPoint()" it returns 0, while in update and the inspector it returns the actual value it should be.
public List<GameObject> waypoints = new List<GameObject>();
public GameObject targetFollow;
int index;
float speed;
public bool settingPoint = false;
private void Start()
{
speed = FindObjectOfType<Player>().GetComponent<Player>().speed;
}
void Update()
{
Debug.Log(index + ": Update");
if (!settingPoint)
{
if(targetFollow != null)
{
if (waypoints.Count > 0 && targetFollow != waypoints[0])
{ targetFollow = waypoints[0]; }
if (transform.position != targetFollow.transform.position)
{ MoveToTarget(); }
else
{
SetNextPoint();
}
}
else
{ SetNextPoint(); }
}
}
private void MoveToTarget()
{
if (targetFollow != null)
{ transform.position = Vector2.MoveTowards(transform.position, targetFollow.transform.position, speed * Time.deltaTime); }
}
public void SetIndex(int index)
{ this.index = index; }
public void AddWaypoint(GameObject newWaypoint)
{
waypoints.Add(newWaypoint);
}
//
// RETURNS 0 IN THE METHOD BELOW
// VVVVVVVVVVVVVVVVVVVVVVVVVVVV
public void SetNextPoint()
{
settingPoint = true;
waypoints.Remove(waypoints[0]);
Debug.LogError(index);
if (index != 0 && waypoints.Count > 0)
{ targetFollow = waypoints[0].gameObject; }
else
{ SetTargetToPreviousBody(); }
settingPoint = false;
}
private void SetTargetToPreviousBody()
{
Debug.LogError(index);
if (index != 0)
{ targetFollow = GetComponent<Player>().bodyParts[index - 1]; }
else { targetFollow = FindObjectOfType<Player>().gameObject; }
}
,I have an int called "index" which is within a script "Body". I assign index in another script when I Instantiate the GameObject it's on. No where else is the value of index changed anywhere though whenever called through a certain method the value always returns 0. I even checked to make sure the value was assigned properly by checking the inspector and calling the value through update by Debug, but any time the value is called through the method it returns 0.
The first script is the "Player" Script which is where I created the new GameObject which has the "Body" script and where "index" is assigned. Index is assigned in the Method "AddBodyPart()"
public GameObject head;
public GameObject bodyPrefab;
public GameObject waypointPrefab;
GameObject lastWaypointPos;
List<GameObject> waypoints = new List<GameObject>();
public List<GameObject> bodyParts = new List<GameObject>();
[SerializeField]int startAmount = 3;
public float speed = 5;
float xDir;
float yDir;
bool blockW = true, blockA, blockS, blockD = false; //Block the inversed direction so you dont go backwards
void Start()
{
lastWaypointPos = null;
blockW = true;
//Starts the Snake moving up
xDir = 0;
yDir = 1;
CreateBody();
}
void Update()
{
ForceMovement();
if(MovementInput())
{ CreateWaypoint(transform.position); }
}
void ForceMovement()
{
//Forces Forward
transform.position += new Vector3(xDir * Time.deltaTime * speed, yDir * Time.deltaTime * speed);
}
private bool MovementInput()
{
//Movement
if (Input.GetKeyDown(KeyCode.W) && !blockW)
{ xDir = 0; yDir = 1; blockS = true; blockA = false; blockD = false; return true; }
else if (Input.GetKeyDown(KeyCode.A) && !blockA)
{ xDir = -1; yDir = 0; blockD = true; blockW = false; blockS = false; return true; }
else if (Input.GetKeyDown(KeyCode.S) && !blockS)
{ xDir = 0; yDir = -1; blockW = true; blockA = false; blockD = false; return true; }
else if (Input.GetKeyDown(KeyCode.D) && !blockD)
{ xDir = 1; yDir = 0; blockA = true; blockW = false; blockS = false; return true; }
else
{ return false; }
}
void CreateWaypoint(Vector2 positon)
{
GameObject waypoint = Instantiate(waypointPrefab, new Vector2(positon.x, positon.y), Quaternion.identity) as GameObject;
lastWaypointPos = waypoint;
AddNewWaypointToBodies(waypoint);
}
void CreateBody()
{
for (int i = 0; i < startAmount; i++)
{
AddBodyPart(i == 0);
bodyParts[i].transform.position -= (Vector3.up * (i + 1)) / 2;
if(i == 0) { bodyParts[0].GetComponent<Body>().targetFollow = head; } //Sets target path to Head
}
}
void AddBodyPart(bool firstBody)
{
GameObject body = Instantiate(bodyPrefab, transform.parent) as GameObject;
bodyParts.Add(body);
body.GetComponent<Body>().SetIndex(bodyParts.Count - 1);
body.name = "body " + bodyParts.Count;
if(!firstBody)
{
body.GetComponent<Body>().waypoints = bodyParts[bodyParts.Count - 2].GetComponent<Body>().waypoints;
if (lastWaypointPos == null) {
body.GetComponent<Body>().targetFollow = bodyParts[bodyParts.Count - 2];
}
else
{
body.GetComponent<Body>().AddWaypoint(lastWaypointPos);
}
}
}
void AddNewWaypointToBodies(GameObject waypoint)
{
for(int i = 0; i < bodyParts.Count; i++)
{
bodyParts[i].GetComponent<Body>().AddWaypoint(waypoint);
}
}
In the "Body" Script I never change its value though when I call it in the Method "SetNextPoint()" it returns 0, while in update and the inspector it returns the actual value it should be.
public List<GameObject> waypoints = new List<GameObject>();
public GameObject targetFollow;
int index;
float speed;
public bool settingPoint = false;
private void Start()
{
speed = FindObjectOfType<Player>().GetComponent<Player>().speed;
}
void Update()
{
Debug.Log(index + ": Update");
if (!settingPoint)
{
if(targetFollow != null)
{
if (waypoints.Count > 0 && targetFollow != waypoints[0])
{ targetFollow = waypoints[0]; }
if (transform.position != targetFollow.transform.position)
{ MoveToTarget(); }
else
{
SetNextPoint();
}
}
else
{ SetNextPoint(); }
}
}
private void MoveToTarget()
{
if (targetFollow != null)
{ transform.position = Vector2.MoveTowards(transform.position, targetFollow.transform.position, speed * Time.deltaTime); }
}
public void SetIndex(int index)
{ this.index = index; }
public void AddWaypoint(GameObject newWaypoint)
{
waypoints.Add(newWaypoint);
}
//
// RETURNS 0 IN THE METHOD BELOW
//
public void SetNextPoint()
{
settingPoint = true;
waypoints.Remove(waypoints[0]);
Debug.LogError(index);
if (index != 0 && waypoints.Count > 0)
{ targetFollow = waypoints[0].gameObject; }
else
{ SetTargetToPreviousBody(); }
settingPoint = false;
}
private void SetTargetToPreviousBody()
{
Debug.LogError(index);
if (index != 0)
{ targetFollow = GetComponent<Player>().bodyParts[index - 1]; }
else { targetFollow = FindObjectOfType<Player>().gameObject; }
}
Your answer
Follow this Question
Related Questions
Problem with int values + 1 gives unexpected values 1 Answer
List.FindIndex 1 Answer
(17,37): error CS0841: A local variable `spawnPointIndex' cannot be used before it is declared 1 Answer
Declared variable being returned as null in a method? 0 Answers
How to make a number higher than Quintillion? (1000000000000000000) 1 Answer