if statement in OnEnable firing inconsistently

C#

I have a little game where the player dodges traffic kind of like an endless frogger level. Currently one script spawns the traffic with the 1st function below. The cars have the 2nd function in a script attached to them. “speed” is a public float set to 3 in this case. aTraffic is a list of pooled cars.

The car’s script has “transform.Translate (speed * Time.deltaTime, 0, 0);” in Update.

The idea is for the script to spawn cars at randomly selected sides of the road(so x= either 5 or -5) and a random y value. If they spawn on the right side, the sprite gets flipped by taking the negative x scale and is told to move in the correct direction by flipping the ‘speed’ variable whenever it enables(3 becomes -3).

This is working MOST of the time, but maybe 10% of the time the speed doesn’t get appropriately set even though checking with Debug.Log shows that OnEnable fired just fine. As a result, I get that ~10% of cars spawning and driving in reverse away from road instead of going across it. It happens to cars spawning on both the left and right.

Any thoughts?

	void OncomingTraffic () {
			
		VehicleDeploying = Random.Range (0, aTraffic.Count);

		if (!aTraffic[VehicleDeploying].activeInHierarchy)
			{
			sidecheck = Random.Range (0, 2);
			

			if (sidecheck == 0) {
				side = -5;
				aTraffic [VehicleDeploying].transform.localScale = new Vector2(1.5f, 1.5f);

			} else {
				side = 5;
				aTraffic [VehicleDeploying].transform.localScale = new Vector2(-1.5f, 1.5f);
			}

			levelCenter = transform.position.y;

			aTraffic[VehicleDeploying].transform.position = new Vector2 (side, Random.Range (levelCenter - 6.5f, levelCenter + 6.5f));
			

			aTraffic[VehicleDeploying].transform.rotation = transform.rotation;

			aTraffic[VehicleDeploying].SetActive(true);

			}

	void OnEnable () {
		

		if (transform.position.x > 0)
			speed = -speed;
		

	}

2 Answers

2

Try logging the position in OnEnable, see what you get. It’s a generally bad idea to leave this kind of logic to events and transforms and the sort. You could make a function in your vehicle (say, WasSpawned(bool bGoingLeft) ) and call it from the traffic manager script. That way you’d have 100% consistent behavior.

Good info, thanks!

I have no baking activated in my game. It's all realtime since there are no non-moving objects.

The problem is probably those two things which doesn’t work very well together:

  • Using speed = -speed;
  • pooling those objects.

If speed is initially set to 3 in the inspector, you can’t be sure what it is when you grab a “pooled” object since the last time the object was in use the speed could have been negative. So flipping the sign again makes it positive when it should be negative and it stays negative when it should be positive.

The probability that this happens should be around 25%

You have several options to solve this. Usually when using an object pool you should care either when you get an object from the pool or if you put it back to reset it’s state to a common state.

In your case you could simply do this:

void OnEnable ()
{
    speed = Mathf.Abs( speed ); // make speed positive
    if (transform.position.x > 0)
        speed = -speed;
}

Wonderful, that makes perfect sense. Thank you. I spent quite awhile trying to tack down the issue but it never occurred to me.