- Home /
the event never ends
The class instance objects powers obstacles enemies according to the position of the character or pj, now the first power that I am instantiating the probe and works well is increases the speed by 5 seconds, now when I enter the method a problem occurs that grabs the initial speed and never returns to the previous speed, it is a "speed runner" so the speed is always constant. I do not have errors in the code but the event does not end. Need the second delegate to run so that it returns to its initial velocity.
this method enters the objects to a queue to be instantiated
public void AddObject(float maxDistanceX)
{
var randomObjIndex = Random.Range(0, this.availableObjects.Count);
var obj = Instantiate(this.availableObjects[randomObjIndex]);
/*
* Here The Problem Power Up this event start and nevers end
*/
if (obj.gameObject.CompareTag("Powerup"))
{
PowerUp pu = obj.AddComponent<PowerUp>();
pu.effect = PowerUpController.powerups["CharacterSpeed"];
}
this.currentObjects.Add(obj);
var position = obj.transform.position;
position.x = maxDistanceX;
position.y = Random.Range(this.minY, this.maxY);
obj.transform.position = position;
}
here is the class powerupcontroller
public class PowerUpController : MonoBehaviour {
const string powerUpPath = "Prefabs/PowerUps/Powerup_";
public GameObject speed;
public static Dictionary<string, Effect> powerups = new Dictionary<string, Effect>()
{
{"2X", new Effect(5f,
delegate { GameController.AddScoreMultiplier(2); },
delegate { GameController.RemoveScoreMultiplier(2); })
},
{"3X", new Effect(5f,
delegate { GameController.AddScoreMultiplier(3); },
delegate { GameController.RemoveScoreMultiplier(3); })
},
{"4X", new Effect(5f,
delegate { GameController.AddScoreMultiplier(4); },
delegate { GameController.RemoveScoreMultiplier(4); })
},
{"CharacterSpeed", new Effect(5f,
delegate { GameController.ChangeCharacterSpeed(15); }, //start
delegate { GameController.ChangeCharacterSpeed(8); }) //end, never execute
}
};
public static IEnumerator CallAfterTime(UnityAction function, float seconds)
{
yield return new WaitForSeconds(seconds);
function();
}
// Update is called once per frame
void Update () {
}
}
and the other class
public class PowerUp : MonoBehaviour {
public Effect effect;
public void OnPickup()
{
effect.action();
effect.endAction += delegate { Remove(); };
StartCoroutine(PowerUpController.CallAfterTime(effect.endAction, effect.length));
}
void OnTriggerEnter2D(Collider2D c)
{
if (c.transform.CompareTag("Player"))
{
print("powerup activado");
OnPickup();
Disable();
}
}
private void Disable()
{
GetComponent<Collider2D>().enabled = false;
GetComponent<SpriteRenderer>().enabled = false;
}
private void Remove()
{
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Question on Events and Delegates 1 Answer
How to unsubscribe from InputSystem event properly 0 Answers
Subscribing to Unity Events with different signatures 1 Answer