Search Unity

Unity Error CS1519

Discussion in 'Scripting' started by edogn1, Nov 17, 2019.

  1. edogn1

    edogn1

    Joined:
    Nov 17, 2019
    Posts:
    2
    I just started learning and I’m trying to test out some JSON stuff to use later and I have no idea what I’m doing wrong.

    Unity gives me: error CS1519: Invalid token ‘=’ in class, struct, or interface member declaration
    These lines specifically.
    Code (CSharp):
    1. starter.name = "Boomer";
    2. starter.health = 10;
    3. starter.attack = 1;
    Code (CSharp):
    1. public class Creature{
    2. public string name;
    3. public int health,attack;
    4. }
    5. Creature starter = new Creature();
    6. starter.name = "Boomer";
    7. starter.health = 10;
    8. starter.attack = 1;
    9. string json = JsonUtility.ToJson(starter);
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    That code looks fine, so I'm guessing it's related to something else. Try posting the entire script, along with the line number the error is occurring on. You'll notice that the compiler error will tell you the line number.
     
  3. edogn1

    edogn1

    Joined:
    Nov 17, 2019
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Main : MonoBehaviour
    7. {
    8.  
    9.     public GameObject MainMenu;
    10.     public GameObject BattleMenu;
    11.     public Button tester;
    12.  
    13.  
    14.  
    15.  
    16.     public void SceneSwitch2Battle(){
    17.         MainMenu.SetActive(false);
    18.         BattleMenu.SetActive(true);
    19.     }
    20.  
    21.  
    22.     public class Creature{
    23.         public string name;
    24.         public int health,attack;
    25.     }
    26.  
    27.     Creature starter = new Creature();
    28.         starter.name = "Boomer";
    29.         starter.health = 10;
    30.         starter.attack = 1;
    31.  
    32.  
    33.     string json = JsonUtility.ToJson(starter);
    34. }
    35.  
    The error says:
    Assets\Main.cs(28,16): error CS1519: Invalid token '=' in class, struct, or interface member declaration
    Then it says the same thing two other times for lines 29 and 30.
     
  4. Deleted User

    Deleted User

    Guest

    This part is not part of a method, Start(), Update() or else; is that normal?
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    As @APSchmidt says, the
    Creature starter = new Creature();
    statement needs to go inside of a method. Right now you just have it directly within the class, which isn't allowed. That could go in Start() or Awake(), or you could create a new method and place that code in it.

    The next question is, are you planning on reusing the "starter" object many times? If so, you'd want to make it a private class variable, and then assign it a value in Start() or Awake().