Unity Error CS1519

Updated on November 18, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on November 17, 2019

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 

The lines  

starter.name = "Boomer";
starter.health = 10;
starter.attack = 1;
Specifically.

 

public class Creature{
 public string name;
 public int health,attack;
 }
Creature starter = new Creature();
 starter.name = "Boomer";
 starter.health = 10;
 starter.attack = 1;
 string json = JsonUtility.ToJson(starter);

 

  • Liked by
Reply
0 on November 18, 2019

You can only set variables (the ‘=’ token) inside a method or function.

 

public class Creature{
    public string name;
    public int health,attack;
 }
public string JsonTest(){
    Creature starter = new Creature();
    starter.name = "Boomer";
    starter.health = 10;
    starter.attack = 1;
    return JsonUtility.ToJson(starter);
}
  • Liked by
Reply
Cancel