简体   繁体   中英

Invalid token ')' and '{' in class, struct, or interface member declaration

The error occurs on all occurrences of '{' and ')' I don't see anything wrong?

// Kills the game object
Destroy{}(gameObject);

// Removes this script instance from the game object
//Destroy(this);

// Removes the rigidbody from the game object
Destroy{}(rigidbody);

// Kills the game object in 5 seconds after loading the object
Destroy{}(5, gameObject);

// When the user presses Ctrl, it will remove the script 
// named FooScript from the game object
void Update()
{
    if (Input.GetButton("Fire1")) 
        GetComponent<FooScript>();
    {
        Destroy(GetComponent<MonoScript>());
    }
}

All those Destroy methods should be part of some method.
They cannot be dangling in a class like that.
In addition, {} are used when opening a new scope, not when calling a method. The code should potentially look like:

// What are these? They should be in some method
void DestroyAll()
{
    // Kills the game object
    Destroy(gameObject);

    // Removes this script instance from the game object
    //Destroy(this);

    // Removes the rigidbody from the game object
    Destroy(rigidbody);

    // Kills the game object in 5 seconds after loading the object
    Destroy(5, gameObject);
}

// When the user presses Ctrl, it will remove the script 
// named FooScript from the game object
void Update()
{
    if (Input.GetButton("Fire1"))
    {
        Destroy(GetComponent<FooScript>());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM