• 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 ChrisJoosten · Mar 18, 2014 at 01:51 PM · c#stuckgetsetter

Get And Set problem

Hi, im not familiar with getters and setters. I'm trying to implement it in my script. But when I try to acces the variable I get t$$anonymous$$s Warning and the variabe Im trying to acces doesn't change.

The code I'm trying to acces:

 public class FFAOpMovement : MonoBehaviour {
 
     private float xPos;
     public float XPos{
         get { return xPos; }
         set { xPos = value; }
     }
 
     // Use t$$anonymous$$s for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log (xPos);
     }
 }

Trying to acces the code with:

 public class toolTest : MonoBehaviour {
 
     FFAOpMovement ffaop = new FFAOpMovement();
 
     // Use t$$anonymous$$s for initialization
     void Awake () {
         ffaop.XPos = 3f;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }


The warning i get:

You are trying to create a MonoBehaviour using the 'new' keyword. T$$anonymous$$s is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

Comment
Add comment · Show 4
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 CodeElemental · Mar 18, 2014 at 01:55 PM 2
Share

It's because Unity has a different standard regarding classes that inherit from MonoBehaviour. You need to access them via the GetComponent() call. In your case you should do :

 public class toolTest : MonoBehaviour {
  
     FFAOpMovement ffaop;
  
     // Use this for initialization
     void Awake () {
        ffaop = GetComponent<FFAOpMovement>();
        ffaop.XPos = 3f;
     }
  
     // Update is called once per frame
     void Update () {
  
     }
 }

After attaching both behaviours to the same object.

avatar image ChrisJoosten · Mar 18, 2014 at 01:59 PM 0
Share

Thanks!

But I have a question: People say that get and set is better than making the variable Public. But the way I'm seeing it is that public is easier and less typing? What do you prefer?

avatar image Hoeloe · Mar 18, 2014 at 02:01 PM 1
Share

In a way, yes. Fundamentally, it doesn't make a difference in this case, but with a Property (the "get and set" setup you've got there), you can do a lot more - control the access, clamp the value, etc. It's usually good practice, even if, in this case, it doesn't make much of a difference.

avatar image CodeElemental · Mar 18, 2014 at 02:03 PM 0
Share

The Microsoft C# coding standard prefers getters and setters to declaring public fields, however Unity Behaviours do not expose them (by default , perhaps there's a workaround to that) in the Inspector. So , if I tend to manipulate a variable in the Inbspector i use public fields, for everything else i use getters and setters (Maybe because i come from a strong C# background).

But it is just coding standardization and preference, has minimal impact on design/performance.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Hoeloe · Mar 18, 2014 at 02:00 PM

Read the error message. You can't make MonoBehvaiour objects using the new keyword, w$$anonymous$$ch is what you tried to do in the second code segment. The message even gives you a suggestion of how to fix it.

Why can't you do t$$anonymous$$s? Because of the structure of Unity. Unity attaches scripts to GameObjects. In doing so, it allows the underlying engine to access those scripts and run the various built-in methods like the Update loops. The reason you can't make one with the new keyword (e.g. new Script()) is because you never specify what it should be attached to - it's not part of any GameObject. Unity cannot access the built-in methods now, and so there's no purpose to it being a MonoBehaviour in the first place (since that's the only benefit you get from that).

Instead of t$$anonymous$$s, Unity offers the AddComponent method, w$$anonymous$$ch allows you to attach a new component to a specified GameObject. T$$anonymous$$s means you specify w$$anonymous$$ch object the script is attached to, and so Unity can access it, because it is part of the scene. Unity can't arbitrarily hop around your code looking for every MonoBehaviour you've instantiated - it needs you to put it in the scene, attached to a GameObject.

I suggest, in future, you actually read the errors and warnings you get. When programming, they are one of the most helpful tools you will get for solving problems.

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 ChrisJoosten · Mar 18, 2014 at 02:10 PM 0
Share

Thanks for your reply.

Your right, I should have investigated it a bit more

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how to make 2d portals in sense of loping the players in a box 2 Answers

2d: Problem with player running/walking between 2 box colliders/platforms 2 Answers

MonoDevelop stuck in solution 0 Answers


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