Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug I need help with a code!

Discussion in 'Editor & General Support' started by Nine079, Feb 26, 2021.

  1. Nine079

    Nine079

    Joined:
    Feb 26, 2021
    Posts:
    9
    When I put this code so that the enemies have life I get this error:
    error CS1001: Identifier expected
    this is the code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class vida_enemigo : MonoBehaviour
    {
    var balas_disparadas = 0;
    var balas_necesarias = 5;

    void OnTriggerEnter(other, Collider)
    {
    if(other.tag == ("bala"))
    {
    balas_disparadas += 1;
    Destroy(other.GameObject);

    if(balas_necesarias == balas_disparadas)
    {
    Destroy(this.GameObject);
    }
    }


    }


    }

    And this is another code in which 2 errors appear:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class disparador : MonoBehaviour
    {
    var projectile, Rigidbody;
    var speed = 20;
    void Update()
    {
    if(Input.GetButtonDown("Fire1"))
    {
    var instantiatedProjectile, Rigidbody = Instantiate(projectile, transform.position, transform.rotation);
    instantiatedProjectile.velocity = transform.TransformDirection( Vector3(0, 0, speed));
    Physics.IgnoreCollision(instantiatedProjectile.GetComponent.<Collider>transform.root.GetComponent.<Collider>);
    }
    }
    }

    the errors are "error CS1001: Identifier expected" and "error CS1525: Invalid expression term ')' "
    help please
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The most important part of an error message is usually the line number it occurs, but you left that part out. Also, please use CODE tags when posting the forum or your code is very difficult to read. See the sticky in the Scripting forum on how to do that.

    But the first error means the compiler is expecting some identifier in your code before some other code it ran into, on whatever line number is in the error message. I'm going to guess it is your OnTriggerEnter declaration, because the compiler has no idea what type the variable "other" is, and for "Collider" you didn't name the variable. See the scripting reference for OnTriggerEnter to see the proper syntax.

    On Invalid expression term ')' it is saying, on whatever line it is complaining about that it ran into a ")" that shouldn't be there. I'm not sure what line that is referring to. But I do notice your Physics.IgnoreCollision line has the GetComponent calls written wrong. You have them written as:
    Code (csharp):
    1. GetComponent.<Collider>
    When proper syntax would be
    Code (csharp):
    1. GetComponent<Collider>()
    See the scripting reference page for GetComponent for more information.

    Also the line above that where you call TransformDirection, you are creating a new Vector3, but you forgot to include the "new" keyword.

    Note that if you have your Visual Studio set up properly, all these syntax errors will be highlighted right in the code. So you don't even need to look for them. Just look at those scripts and you'll see red squiggly lines at every error you need to fix. Fix all the red, file/save, then back to Unity. If you're not using a proper code editor, start.
     
  3. Nine079

    Nine079

    Joined:
    Feb 26, 2021
    Posts:
    9
    Thank you very much for the help, but I don't understand how to create the variable for "other" and "collider"
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Did you look at the scripting reference for OnTriggerEnter? The example I think is pretty clear. I'll copy it here for you to look at how you are declaring that method, and compare to how the example does.

    The top line below is what I'm talking about. The example below is written correctly, while yours is not.

    Code (csharp):
    1.    private void OnTriggerExit(Collider other)
    2.    {
    3.        switch (colorPicker)
    4.        {
    5.            case 0: rend.material.color = Color.white; break;
    6.            case 1: rend.material.color = Color.cyan; break;
    7.            case 2: rend.material.color = Color.blue; break;
    8.            case 3: rend.material.color = Color.black; break;
    9.            case 4: rend.material.color = Color.red; break;
    10.            case 5: rend.material.color = Color.green; break;
    11.            case 6: rend.material.color = Color.grey; break;
    12.            case 7: rend.material.color = Color.magenta; break;
    13.            case 8: rend.material.color = Color.yellow; break;
    14.            case 9: rend.material.color = Color.gray; break;
    15.        }
    16.    }
    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

    I'm not sure that is the only issue in your code though, as it is difficult to read unformatted. Note how the code above is as easy to read as in a code editor.
     
  5. Nine079

    Nine079

    Joined:
    Feb 26, 2021
    Posts:
    9
    Thanks, but now I don't know why I have an error with the void in the same line: "(12,4): error CS1519: Invalid token 'void' in class, struct, or interface member declaration"
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It is a new error, so what does your code look like now?
     
  7. Nine079

    Nine079

    Joined:
    Feb 26, 2021
    Posts:
    9
    I only changed 2 lines of code and these are:
    GetComponent <Collider>

    void OnTriggerEnter(Collider other)

    I added the first one, and if I remove it, 7 more errors appear
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can you just post the entire script in its current state using CODE tags please? The error can be caused by something earlier in the script, such as if immediately above this line you close out the class for example.
     
  9. Nine079

    Nine079

    Joined:
    Feb 26, 2021
    Posts:
    9
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class vida_enemigo : MonoBehaviour
    7. {
    8.    var balas_disparadas = 0;
    9.    var balas_necesarias = 5;
    10.    GetComponent <Collider>
    11.  
    12.    void OnTriggerEnter(Collider other)
    13.     {
    14.         if(other.tag == ("bala"))
    15.         {
    16.             balas_disparadas += 1;
    17.             Destroy(other.GameObject);
    18.  
    19.            if(balas_necesarias == balas_disparadas)
    20.             {
    21.                 Destroy(this.GameObject);
    22.             }
    23.         }
    24.  
    25.      
    26.     }
    27.  
    28.  
    29. }
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah your problem is actually line 10. You can't have GetComponent <Collider> just hanging out there outside of any method. You delete line 10, and line 12 is no longer a problem.

    When I originally mentioned GetComponent, I was referring to your second script, on the line that starts with Physics.IgnoreCollision.
     
  11. Nine079

    Nine079

    Joined:
    Feb 26, 2021
    Posts:
    9
    ok thanks but now I get errors with all the lines that say "var"
    errors:
    (8,1): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code
    (8,4): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code
    (9,4): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code
    (9,1): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code
     
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  13. Nine079

    Nine079

    Joined:
    Feb 26, 2021
    Posts:
    9
    now i change it in both scripts and i have 5 errors:
    errors:
    disparador.cs(14,17): error CS0819: Implicitly-typed variables cannot have multiple declarators
    disparador.cs(14,21): error CS0818: Implicitly-typed variables must be initialized
    disparador.cs(14,69): error CS1503: Argument 1: cannot convert from 'int' to 'UnityEngine.Object'
    vida_enemigo.cs(17,27): error CS1061: 'Collider' does not contain a definition for 'GameObject' and no accessible extension method 'GameObject' accepting a first argument of type 'Collider' could be found (are you missing a using directive or an assembly reference?)
    vida_enemigo.cs(21,30): error CS1061: 'vida_enemigo' does not contain a definition for 'GameObject' and no accessible extension method 'GameObject' accepting a first argument of type 'vida_enemigo' could be found (are you missing a using directive or an assembly reference
    the code:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class vida_enemigo : MonoBehaviour
    7. {
    8.    int balas_disparadas = 0;
    9.    int balas_necesarias = 5;
    10.  
    11.  
    12.    void OnTriggerEnter(Collider other)
    13.     {
    14.         if(other.tag == ("bala"))
    15.         {
    16.             balas_disparadas += 1;
    17.             Destroy(other.GameObject);
    18.  
    19.            if(balas_necesarias == balas_disparadas)
    20.             {
    21.                 Destroy(this.GameObject);
    22.             }
    23.         }
    24.  
    25.        
    26.     }
    27.    
    28.  
    29. }
    30.  
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class disparador : MonoBehaviour
    7. {
    8. int projectile, Rigidbody;
    9. int speed = 20;
    10.     void Update()
    11.     {
    12.          if(Input.GetButtonDown("Fire1"))
    13.             {
    14.                 var instantiatedProjectile, Rigidbody = Instantiate(projectile, transform.position, transform.rotation);
    15.                 instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
    16.                 Physics.IgnoreCollision(instantiatedProjectile.GetComponent<Collider>(), transform.root.GetComponent<Collider>());
    17.             }
    18.     }
    19. }
    20.  
     
  14. Nine079

    Nine079

    Joined:
    Feb 26, 2021
    Posts:
    9
    I think I should completely change both scripts as they have a lot of bugs
     
  15. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you just copying this code from somewhere? Because you're making very basic C# syntax errors, but you're writing code here that is fairly complicated. Do you understand what this code is doing?

    In disparador, you're now declaring a variable Rigidbody as type int, but Rigidbody is itself a type. I don't even know what you're trying to do on line 14. You should just have a single variable to the left of that equals sign.

    I'd really recommend going through C# tutorials or working with a book so you understand C# syntax, because with C# you have to write every character exactly correct. A single wrongly placed character and your code won't compile, and I can't teach that stuff over a forum comment.

    But use a code editor which highlights errors. Then you rarely even write them, because as soon as your fingers put them down, they are highlighted as an error. Then you don't move on to write other errors, until you fix it. You shouldn't end up with this many errors all over your code, because your scripts would be a sea of red.
     
  16. Nine079

    Nine079

    Joined:
    Feb 26, 2021
    Posts:
    9
    okay, you're right, I'm going to follow the basic unity code tutorials, I just wanted to test if this works, thank you very much