- Home /
Overwrite the value in my List but why?
Hi, I create a field of tiles. Every tile is store in a class who also have a list for all neighbours and every neighbour have a float for movement cost. The cost should be different but it looks like the class overwrite the values from the tile befor. I try to make a seperate list for every single node but looks like the value of movement cost is always overwrite but I can't figure out why.
Here the class for the node:
public class Node {
public Transform tileTrans;
public List<Node> neighbours;
public float movementCost;
public Renderer ren;
// public Node()
// {
// neighbours = new List<Node> ();
// }
}
And here the class who seek the neigbours and store it in the list: This class look for every tile and find via raycast the tiles next to it. If the neighbor tile diagonal to it, the neighbor get a movementCost of 1.4f. But when I test the code all neighbors have a value of 1.4f.
using UnityEngine;
using System.Collections.Generic;
public class MapManager : MonoBehaviour {
//variables
List<Node> graph;
LayerMask mask = 14;
void Awake()
{
graph = new List<Node> ();
GetAllNodesInGraph ();
GetNeighbours ();
}
void GetAllNodesInGraph()
{
GameObject[] tiles = GameObject.FindGameObjectsWithTag ("Tile");
foreach (GameObject go in tiles)
{
Node n = new Node();
n.tileTrans = go.transform;
n.ren = go.GetComponent<Renderer>();
graph.Add (n);
}
}
void GetNeighbours ()
{
foreach (Node n in graph)
{
List<Node> neighbours = new List<Node>();
Vector3 originPos = new Vector3(n.tileTrans.position.x, n.tileTrans.position.y + 0.5f, n.tileTrans.position.z);
RaycastHit hitInfo;
if(Physics.Raycast(originPos + new Vector3(1f, 0f, 0f), -Vector3.up, out hitInfo, mask.value ))
{
foreach(Node hit in graph)
{
if(hit.tileTrans == hitInfo.transform)
{
neighbours.Add(hit);
hit.movementCost = 1f;
}
}
}
if(Physics.Raycast(originPos + new Vector3(-1f, 0f, 0f), -Vector3.up, out hitInfo, mask.value ))
{
foreach(Node hit in graph)
{
if(hit.tileTrans == hitInfo.transform)
{
neighbours.Add(hit);
hit.movementCost = 1f;
}
}
}
if(Physics.Raycast(originPos + new Vector3(0f, 0f, 1f), -Vector3.up, out hitInfo, mask.value ))
{
foreach(Node hit in graph)
{
if(hit.tileTrans == hitInfo.transform)
{
neighbours.Add(hit);
hit.movementCost = 1f;
}
}
}
if(Physics.Raycast(originPos + new Vector3(0f, 0f, -1f), -Vector3.up, out hitInfo, mask.value ))
{
foreach(Node hit in graph)
{
if(hit.tileTrans == hitInfo.transform)
{
neighbours.Add(hit);
hit.movementCost = 1f;
}
}
}
if(Physics.Raycast(originPos + new Vector3(-1f, 0f, 1f), -Vector3.up, out hitInfo, mask.value ))
{
foreach(Node hit in graph)
{
if(hit.tileTrans == hitInfo.transform)
{
neighbours.Add(hit);
hit.movementCost = 1.4f;
}
}
}
if(Physics.Raycast(originPos + new Vector3(1f, 0f, 1f), -Vector3.up, out hitInfo, mask.value ))
{
foreach(Node hit in graph)
{
if(hit.tileTrans == hitInfo.transform)
{
neighbours.Add(hit);
hit.movementCost = 1.4f;
}
}
}
if(Physics.Raycast(originPos + new Vector3(-1f, 0f, -1f), -Vector3.up, out hitInfo, mask.value ))
{
foreach(Node hit in graph)
{
if(hit.tileTrans == hitInfo.transform)
{
neighbours.Add(hit);
hit.movementCost = 1.4f;
}
}
}
if(Physics.Raycast(originPos + new Vector3(1f, 0f, -1f), -Vector3.up, out hitInfo, mask.value ))
{
foreach(Node hit in graph)
{
if(hit.tileTrans == hitInfo.transform)
{
neighbours.Add(hit);
hit.movementCost = 1.4f;
}
}
}
n.neighbours = new List<Node> (neighbours);
}
}
public List<Node> GetGraph()
{
return graph;
}
}
I guess I do not understand the behavior of List<> and make values store to a List<> seperate and not refer to a value.
Maybe you can help.
Your answer
Follow this Question
Related Questions
How can I make a list of Classes or Scripts? 3 Answers
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
How to initialize lists with set number of null elements? 1 Answer