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
5
Question by MvD · Apr 20, 2011 at 01:48 PM · bugcompilerinitialization

Microsoft vs Mono C# compiler initialisation differences. Is this a bug in Mono?

Hello fellow coders! I have the following code:

using System; namespace Test { class Test { public bool A = true; }

 class Program
 {
     static void Main(string[] args)
     {
         var test = new Test
         {
             A = false
         };

         Console.WriteLine("test.A = " + test.A);
     }
 }

}

When I compile this C# code into a Visual Studio console executable, I get: "test.A = False". However, in Unity 3.3.0f4 (after I delete the namespace bit and change Console.WriteLine to Debug.Log) I get: "test.A = True"

My question: is this a bug in Mono? (I'd expect the VS behaviour)

Comment
Add comment · Show 1
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 Adrian · Aug 01, 2011 at 10:44 AM 0
Share

I've stumbled upon the same issue (Unity 3.4) but with float/int ins$$anonymous$$d of bool. In this case, if the field initializer sets the value to 1, it's not possible to set it to 0 in the object initializer. Any value other than 0 works and if the initial value is 0, it can be set to 1.

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Mike 3 · Apr 20, 2011 at 02:11 PM

Looks like a mono bug, if you use properties instead of public fields it works fine:

using UnityEngine;

public class TestBug : MonoBehaviour { void Start() { var test = new Testee() { A = false }; Debug.Log("test.A = " + test.A); // Prints False, as expected. } }

class Testee { private bool a = true; public bool A { get { return a; } set { a = value; } } }

Comment
Add comment · Show 4 · 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 Statement · Apr 20, 2011 at 02:40 PM 0
Share

Yeah the property seems to work just fine.

avatar image MvD · Apr 20, 2011 at 03:47 PM 0
Share

That's true, but I'd like to have a default value for A defined in the class, and then override this sometimes when creating an object. And I cannot set A to its default value in the constructor with an ordinary assignment, because that happens after processing the initialiser list.

avatar image Mike 3 · Apr 20, 2011 at 04:38 PM 1
Share

$$anonymous$$odified my answer - same principle, but uses a backing field ins$$anonymous$$d

avatar image Agamemjohn · Sep 14, 2012 at 09:57 PM 0
Share

I'd like to add that I believe it's a Unity bug (since, if you make a straight $$anonymous$$ono project in $$anonymous$$onoDevelop and run the same code (obviously with different logging methods), it works as expected.

avatar image
3

Answer by Statement · Apr 20, 2011 at 02:18 PM

Indeed, I get the same strange behavior in Unity3D.

using UnityEngine;

public class Test : MonoBehaviour { void Start() { var test = new Testee() { A = false }; Debug.Log("test.A = " + test.A); // Prints True, not False as expected. } }

class Testee { public bool A = true; }

...And the behavior in a VS Console App prints False, not True.

I haven't got the slightest clue what Mike was talking about, but it sounds like a bug to me.

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 Jessy · Apr 20, 2011 at 02:25 PM 0
Share

A man with your reputation shouldn't be calling Unity that! :-(

avatar image Statement · Apr 20, 2011 at 02:35 PM 0
Share

Why, is there an ambiguity somewhere?

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

2 People are following this question.

avatar image avatar image

Related Questions

Bug hunting in the code... 1 Answer

Object initializer Compiler BUG? 1 Answer

my mistake or bug? Unity compiler error 0 Answers

Initialising List array for use in a custom Editor 1 Answer

Bug with Unity Compiler with Lambdas? 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