Search Unity

Assets/Scripts/weaponSwitch.cs(39,1): error CS1525: Unexpected symbol `}'

Discussion in 'Scripting' started by NovaTheThird, Mar 12, 2017.

  1. NovaTheThird

    NovaTheThird

    Joined:
    Mar 12, 2017
    Posts:
    1
    So I am trying to create a simple gun switch script, following a tutorial. I'm starting out with only the glock and AK for now, but I will add more once I get the first 2 done. (I am very new to coding.) Whenever I try anything regarding it, I just create more errors. I am seeking help, if anyone can!
    Here is the video:

    Here is the script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class weaponSwitch : MonoBehaviour {
    6.     public GameObject Glock18;
    7.     public GameObject AK47;
    8.     public GameObject M4Carbine;
    9.     public GameObject L96;
    10.     public GameObject Shotgun;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (Input.GetKeyDown (KeyCode.E)) {
    20.             RaycastHit hit;
    21.             if (Physics.Raycast (Camera.main.transform.forward, transform.forward, out hit));
    22.                 if (hit.transform.tag == "item")
    23.                     if(hit.transform.gameObject.name.Contains("World Model Glock-18")){
    24.                         if(weaponOut==1){
    25.                             changeWeapon(1);
    26.                             if (weaponOut == 1) {
    27.                                 Destroy (hit.transform.gameObject);
    28.                                 Instantiate (Glock18, transform.root.transform.position + new Vector3 (0, 2, 0), Quaternion.identity);
    29.                             }else if(weaponOut==2){
    30.                                     Destroy(hit.transform.gameObject);
    31.                                     Instantiate(AK47, transform.root.transform.position+new Vector3(0, 2, 0), Quaternion.identity);
    32.  
    33.                             }
    34.                         }
    35.                     }
    36.                 }
    37.             }
    38.         }
    39.     }
    40.     void changeWeapon(int weapon){
    41.         if (weaponIn == 1) {
    42.             weaponOut == 1;
    43.             transform.FindChild ("World Model Glock-18").gameObject.SetActive (false);
    44.             transform.FindChild ("World Model AK-47").gameObject.SetActive (true);
    45.         } else if (weaponIn == 2)
    46.             weaponOut == 2;
    47.         transform.FindChild ("World Model Glock-18").gameObject.SetActive (true);
    48.         transform.FindChild ("World Model AK-47").gameObject.SetActive (false);
    49.  
    50. }
     
  2. JeffCarvalho

    JeffCarvalho

    Joined:
    Mar 12, 2017
    Posts:
    1
    i found one of the problems, you were closing your class on the line 39, so every line from 40 was wrong in the syntax.

    this is a fairly easy mistake to catch, seems like you are really new to code.

    if i may, i suggest you to practice the basics a little more, before trying something more complex, here some links
    https://msdn.microsoft.com/en-us/library/dd492171.aspx
    http://holowczak.com/programming-concepts-tutorial-programmers/

    I tried to debug your code, even not knowing the context so, it maybe still wrong. here you go:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class weaponSwitch : MonoBehaviour {
    5.  
    6.     public GameObject Glock18;
    7.     public GameObject AK47;
    8.     public GameObject M4Carbine;
    9.     public GameObject L96;
    10.     public GameObject Shotgun;
    11.     // Use this for initialization
    12.     void Start () {
    13.     }
    14.     // Update is called once per frame
    15.     void Update () {
    16.  
    17.         if (Input.GetKeyDown (KeyCode.E)) {
    18.             RaycastHit hit;
    19.  
    20.             if (Physics.Raycast (Camera.main.transform.forward, transform.forward, out hit)){
    21.  
    22.                 if (hit.transform.tag == "item"){
    23.  
    24.                     if(hit.transform.gameObject.name.Contains("World Model Glock-18")){
    25.  
    26.                         if(weaponOut==1){
    27.  
    28.                             changeWeapon(1);
    29.  
    30.                             if (weaponOut == 1) {
    31.                                 Destroy (hit.transform.gameObject);
    32.                                 Instantiate (Glock18, transform.root.transform.position + new Vector3 (0, 2, 0), Quaternion.identity);
    33.                             }else if(weaponOut==2){
    34.                                     Destroy(hit.transform.gameObject);
    35.                                     Instantiate(AK47, transform.root.transform.position+new Vector3(0, 2, 0), Quaternion.identity);
    36.                             }
    37.                         }
    38.                     }
    39.                 }
    40.             }
    41.         }
    42.     }
    43.  
    44.     void changeWeapon(int weapon){
    45.  
    46.         if (weaponIn == 1) {
    47.             weaponOut == 1;
    48.             transform.FindChild ("World Model Glock-18").gameObject.SetActive (false);
    49.             transform.FindChild ("World Model AK-47").gameObject.SetActive (true);
    50.         } else if (weaponIn == 2){
    51.             weaponOut == 2;
    52.      
    53.         transform.FindChild ("World Model Glock-18").gameObject.SetActive (true);
    54.         transform.FindChild ("World Model AK-47").gameObject.SetActive (false);
    55.  
    56.         }
    57.     }
    58.  
    59. }
    Good luck!