- Home /
Get Set Errors C#
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; }
}
}
Answer by dannyskim · Oct 22, 2012 at 04:43 AM
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.
Are there get and set functions for lists of bools? I have a list similar to this one but it's a bool.
It's quite the opposite, it's rather "pointfull", lol.
I suggest you read up on what properties are, and in this case, exactly what accessors are:
http://msdn.microsoft.com/en-us/library/aa287786(v=vs.71).aspx
When you think about it, the point of getters / setters is so that you variable member can still retain all the functionality of a regular method, without declaring another method.
So, just to give you an example:
public List ExampleListSave { get { return _ExampleListSave; Debug.Log( "_ExampleList has been accessed." ) } set { _ExampleList = value; Debug.Log( "ExampleList has been set" ); } }
The simplest use case is say your variable was an int ins$$anonymous$$d, and that int was say the health of the character that can't go over 100 or below 0. So
private int _health;
public int Health { get { return _health; } set { if( value > 100 ) _health = 100; else if( value < 0 ) _health = 0; else _health = value; } }
Answer by phodges · Oct 22, 2012 at 08:17 AM
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; }
}
/ can you also help me with my script ***receiving error "; expected" and get/set does not exist in the current context
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraFollow : $$anonymous$$onoBehaviour { private Transform player; private float damping = 2f; private float height = 7f; private Vector3 startPos; private bool canFollow;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindWithTag("Player").transform;
startPos = transform.position;
canFollow = true;
}
// Update is called once per frame
void Update()
{
Follow();
}
void Follow()
{
if (canFollow)
{
transform.position = Vector3.Lerp(transform.position, new Vector3(player.position.x + 10f, player.position.y + height, player.position.z - 10f), Time.deltaTime * damping);
}
}
public bool CanFollow() { get { return canFollow; } set { canFollow = value; }
}
} // class