Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ochreous · Oct 22, 2012 at 04:01 AM · c#

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; }
    }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ochreous · Oct 22, 2012 at 05:07 AM 0
Share

Are there get and set functions for lists of bools? I have a list similar to this one but it's a bool.

avatar image dannyskim · Oct 22, 2012 at 05:10 AM 0
Share

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; } }

avatar image
0

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; }
   
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image blackkeath88 · Dec 13, 2019 at 02:13 AM 0
Share

/ 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check if an object is colliding with another from another script ? 1 Answer

Finding Distance between Angles and Points 2 Answers

How to create an Idle counter ? 2 Answers

C# OnControllerColliderHit Detection Problems 0 Answers

C# OnMouseExit Kill/Destroy GUILayout Element 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges