Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
help i got an error called "CS1022" how do i fix it? it is in line 21,1 how to fix D:

C#
  1  using System.Collections;
  2  using System.Collections.Generic;
  3  using UnityEngine;
  4  
  5  public class WeaponScript : MonoBehaviour {
  6  
  7      public GameObject Weapon;
  8      public GameObject Postion;
  9      public AudioSource KnifeGrab;
 10      public bool KnifePickedUp;
 11  
 12      void Update()
 13      {
 14  
 15              if (Input.GetKeyDown("f")) 
 16              {
 17               Postion.SetActive (false);}}
 18               public bool KnifePickedUp = false;
 19              }
 20  
 21  if (Input.GetKeyDown("f")) 
 22              {
 23               Weapon.SetActive (true);
 24               public bool KnifePickedUp = true;
 25               KnifeGrab.Play();
 26              }
 27  
 28              if (Input.GetKeyDown("2")) 
 29              {
 30               Weapon.SetActive (true);
 31               public bool KnifePickedUp = true;
 32               KnifeGrab.Play();
 33              }
 34  
 35              if (Input.GetKeyDown("1")) 
 36              {
 37               Weapon.SetActive (false);
 38               public bool KnifePickedUp = false;
 39              }
 40      }
 41  
 42      public void playSoundEffect()
 43      {
 44             if (Input.GetKeyDown("f") && public bool KnifePickedUp = true;) 
 45             {
 46              KnifeGrab.Play();
 47             }
 48      }
 49  }


What I have tried:

i dont understand how to fix D:
Posted
Updated 30-Dec-21 8:40am
v4

Advice: use a CS beautifier, problem gets obvious.
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponScript: MonoBehaviour {

  public GameObject Weapon;
  public GameObject Postion;
  public AudioSource KnifeGrab;
  public bool KnifePickedUp;

  void Update() {

    if (Input.GetKeyDown("f")) {
      Postion.SetActive(false);
    }
  }
  public bool KnifePickedUp = false;
}

if (Input.GetKeyDown("f")) {
  Weapon.SetActive(true);
  public bool KnifePickedUp = true;
  KnifeGrab.Play();
}

if (Input.GetKeyDown("2")) {
  Weapon.SetActive(true);
  public bool KnifePickedUp = true;
  KnifeGrab.Play();
}

if (Input.GetKeyDown("1")) {
  Weapon.SetActive(false);
  public bool KnifePickedUp = false;
}
}

public void playSoundEffect() {
  if (Input.GetKeyDown("f") && public bool KnifePickedUp = true;) {
    KnifeGrab.Play();
  }
}
}

https://codebeautify.org/csharpviewer[^]
 
Share this answer
 
v2
Comments
0x01AA 30-Dec-21 15:08pm    
Cool one, a 5. Do you have a link for an online tool?
0x01AA 30-Dec-21 15:17pm    
thumbs up
Patrice T 30-Dec-21 15:23pm    
Thank you
Have a look on line 17:

15              if (Input.GetKeyDown("f"))
16              {
17               Postion.SetActive (false);}}        // <<< Check this ;)
18               public bool KnifePickedUp = false;
19              }
 
Share this answer
 
drop those }} in line 17 !

due to the extraneous }} the } in line 19 closes the last open { resulting in unexpected text in line 21

:)
 
Share this answer
 
Comments
Luc Pattyn 30-Dec-21 14:05pm    
Of course, you can't declare a class-level member in the middle of an if statement. Make up your mind, decide what you want, and make sure your indentation matches your intentions, so you don't confuse yourself and others...
Satsuei-Dev 30-Dec-21 14:09pm    
Assets\WeaponScript.cs(16,14): error CS1513: } expected i fixed it but now it says this D:
Nobody looks at the error numbers. The entire error message is important, including the line numbers they occur on.

You have to make sure your braces are paired up. Every opening brace needs a closing brace.

You should pick a single brace style and stick with it. You have a combination of styles where the opening brace is either on the same line as a line of code and where it's on its own line.

This also applies to parenthesis. And in your code, that's where it's screwed up.
C#
public void playSoundEffect()
{
       if (Input.GetKeyDown("f") && public bool KnifePickedUp = true;)
       {
        KnifeGrab.Play();
       }
}

The bolded portion is defining a variable, but that's not legal in an if condition expression. I'd guess that you're looking for this instead:
C#
public void playSoundEffect()
{
       if (Input.GetKeyDown("f") && KnifePickedUp = true)
       {
        KnifeGrab.Play();
       }
}
 
Share this answer
 
Comments
0x01AA 30-Dec-21 15:24pm    
Quote: "You have a combination of styles where the opening brace is either on the same line as a line of code and where it's on its own line."
No!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900