float value is reset after every update
In the foor loop below I have two prints, one before and one after the update, but they still print the same thing. why?
Time.deltaTime: returns different floats each update, mostly around 0.016f - 0.018f. citiesList.Count: is currently set to 1.
citiesList[cityIndex].myCitizens.Count: is currently set to 1.
myWanderTime: Is set to 0.0f at start, updates with deltaTime, but is reset to 0.0f after the Update function finishes.
myCityPosition: is set to {0.0f, 0.0f, 0.0f}.
void Update()
{
for (int cityIndex = 0; cityIndex < citiesList.Count; ++cityIndex)
{
for (int citizenIndex = 0; citizenIndex < citiesList[cityIndex].myCitizens.Count; ++citizenIndex)
{
print("Before: " + citiesList[cityIndex].myCitizens[citizenIndex].myPosition.x);
citiesList[cityIndex].myCitizens[citizenIndex].Update(Time.deltaTime);
print("After: " + citiesList[cityIndex].myCitizens[citizenIndex].myPosition.x);
}
}
}
The Citizen Update function
public void Update(float aDeltaTime)
{
myWanderTime += aDeltaTime;
float newXPos = myCityPosition.x + Mathf.Cos(myWanderTime) * 5.0f;
float newZPos = myCityPosition.z + Mathf.Sin(myWanderTime) * 5.0f;
Vector3 newPos = new Vector3(newXPos, 0.0f, newZPos);
myPosition = newPos;
myModel.transform.position = myPosition;
}
Answer by Twokarasu · Mar 29, 2018 at 12:32 PM
Problem solved.
A structure is a value type so it is stored on the stack, but a class is a reference type and is stored on the heap. A structure doesn't support inheritance, and polymorphism, but a class supports both. ... As a structure is a value type, we can't assign null to a struct object, but it is not the case for a class.
https://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net