Search Issue Tracker

Postponed means that the issue was either a feature request or something that requires major refactoring on our side. Since that makes the issue not actionable in the close future we choose to close it as Postponed and add it on our internal roadmaps and technical debt pages instead.

Postponed

Votes

3

Found in

4.3.4f1

Issue ID

592094

Regression

No

Object member initializers in C# scripts ignores values that are same default value of that type

Scripting

-

Example:

public class SomeClass
{
public int SomeValue = 1;
}

SomeClass a = new SomeClass { SomeValue = 0 };
Debug.Log(a.SomeValue); // 1

To reproduce:
1) Open the attached scene and load 'test' scene
2) Play the scene and go to console tab
3) Notice that second line outputs 'b: 1 3 hehe' instead of 'b: 0 3 ', even though it is written in the code that it should change this value.

Comments (1)

  1. Neat-Sketch

    Aug 18, 2015 15:41

    I encountered this issue while working on my project too. It's a serious bug because a buggy programming language feature can potentially lead to even more serious problems.

    Here is another example of this issue. Just attach the following script to any active GameObject. You will see the result in the log.

    using UnityEngine;

    public class MyClass {
    public int a = -1;
    public int b = -1;
    public int c = -1;
    public int d = -1;
    }

    public class TestScript : MonoBehaviour {

    void Start () {

    MyClass myObject = new MyClass {
    a = -1,
    b = 0,
    c = 42,
    d = 5
    };

    Debug.Log (myObject.a); // prints -1
    Debug.Log (myObject.b); // prints -1 (Should be 0 !!!)
    Debug.Log (myObject.c); // prints 42
    Debug.Log (myObject.d); // prints 5

    }

    }

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.