I know how to fix parsing errors but this one is giving me too much trouble. I know I have to add curly brackets to close the class but it won’t work for me. Here’s my script and I am also using C# if anyone wants to know:`
public class PowerupScript : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
Destroy (gameObject);
{
if(collectedItem){
respawnTimer += Time.deltaTime;
if(respawnTimer > delayTime){
var newObject = Instantiate(objectPrefab, transform.position, transform.rotation);
respawnTimer = 0.0;
}
}`
In that code the Destroy is the only thing controlled by the if, the rest always happens and the { } around the rest are meaningless. I assume what you want is either:
First issue, you don’t have enough closing braces (curly brackets). There should be one } for every {
Currently you have 5 opening braces, and only 2 closing ones.
To simply make it parse correctly, you could just add 3 more closing braces so you have 5.
The second issue is that you might have some in the wrong place, specifically the one after if(other.tag == "Player"), you have a single line (Destroy(gameObject)) which is run if the other object is a player.
The next set of braces (which contain if collectedItem and the respawn code) get executed regardless as they have no if () before them.
When the braces line up correctly it’s much easier to see which blocks of code go together.
At a guess, this is the final code you want…
public class PowerupScript : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Destroy (gameObject);
if ( collectedItem ) {
respawnTimer += Time.deltaTime;
if ( respawnTimer > delayTime ) {
var newObject = Instantiate(objectPrefab, transform.position, transform.rotation);
respawnTimer = 0.0;
}
}
}
}