Is there any way in this situation to call the value stored onto pos3 in the first loop from the second one? Ive tried assigning it to a separate variable but that doesnt seem to work either. Sorry if my phrasing is a little weird I dont really know how to properly describe the problem.
Vector3Int pos3;
for (int x = 0; x < Mathf.Abs(xDist); x++)
{
pos3 = new Vector3Int(pos1.x + (x * x1), pos1.y, 0);
}
for (int y = 0; y < Mathf.Abs(yDist); y++)
{
//then call pos3 here
}
What have you tried? It should work just fine since âpos3â is declared outside of both loops.
Vector3Int pos3;
for (int x = 0; x < Mathf.Abs(xDist); x++)
{
pos3 = new Vector3Int(pos1.x + (x * x1), pos1.y, 0);
}
for (int y = 0; y < Mathf.Abs(yDist); y++)
{
Debug.Log("I can access " + pos3 + " from here!");
}
That being said your first loop doesnât make a lot of sense to me. If all youâre doing is reassigning data to the same pos3 variable, then thereâs no point in having a loop, since all of your efforts throughout the loop will just be overwritten by the final iteration of the loop.
I tried to call pos3 directly after the first loop with debug.log to see what value was stored in it, but it gave me an error message saying âuse of unasigned local variable âpos3ââ. I think in that debug.log, it is trying to call the value from when i first declared pos3.
Sorry, there is a bit more context to the code, but I cut it out because it didnt affect the problem. heres the full code:
Vector3Int pos3;
for (int x = 0; x < Mathf.Abs(xDist); x++)
{
pos3 = new Vector3Int(pos1.x + (x * x1), pos1.y, 0);
main.SetTile(pos3, floor); //main.SetTile(new Vector3Int(pos1.x + x, pos1.y, pos1.z), floor);
}
Debug.Log(pos3);
for (int y = 0; y < Mathf.Abs(yDist); y++)
{
pos3 = new Vector3Int(pos1.x, pos1.y + (y * y1), 0);
Thatâs because the compiler canât be sure that you have assigned a value to pos3. Itâs possible that Mathf.Abs(xDist) is 0 or less, so that the loop doesnât run even once and pos3 remains unassigned. You can fix this by assigning a default value when you declare pos3:
Vector3Int pos3 = default;
// or
Vector3Int pos3 = Vector3Int.zero;