Hi everyone, I keep getting this error when I try to get and set my List error unexpected symbol {. Why is it doing this?
using UnityEngine;
using System.Collections;
Public class ExampleScript:Monobehaviour{
[System.Serializable]
public class IntValueClass
{
public int IntValue = 0;
public IntValueClass(int start_value)
{
IntValue = start_value;
}
}
public List<IntValueClass> ExampleList = new List<IntValueClass>();
public List<IntValueClass> ExampleListSave = new List<IntValueClass>();
//error unexpected symbol {
{
get { return _ExampleListList; }
set {_ExampleList = value; }
}
}
When writing a get / set, I don’t believe you can initialize it with a value.
On top of that, you have one too many curly brackets…
public List<IntValueClass> ExampleListSave
{
get { return _ExampleListList; }
set {_ExampleList = value; }
}
Is what it should look like. When you access the public member ExampleListSave, it’s going to return _ExamleListList, which has already been initialized, so you don’t need to initialize it again, or rather, you can’t.
Is there a reason you can’t make your assignment in the constructor, like this?
using UnityEngine;
using System.Collections.Generic;
public class ExampleScript : MonoBehaviour
{
[System.Serializable]
public class IntValueClass
{
public int IntValue = 0;
public IntValueClass(int start_value)
{
IntValue = start_value;
}
}
public ExampleScript()
{
ExampleListSave = new List<IntValueClass>();
}
public List<IntValueClass> ExampleListSave { get; set; }
}