3

I don't know why my script below in unity c# always got null ?

public class WeatherControl : MonoBehaviour {
    public GameObject Rain;

    public int[] RainTime = new int[]{6,7,8,9,10,18,19,20,21,22,16};

    int day;
    System.DateTime dates;

    // Use this for initialization
    void Start () {
        dates = System.DateTime.UtcNow;
        day = (int) dates.Day;
        //day = 16;
        Debug.Log ("DAY : " + day);
        int posRain = System.Array.FindIndex (RainTime, x => x.Equals(16));

        Debug.Log ("POS RAIN  : " + posRain);
        if (posRain >= 0) {
            Rain.SetActive (true);
        } else {
            Rain.SetActive (false);
        }

    }

}

my variable int posRain always return -1 Even at array there is value contain it. the variable day contain 16. I put it manually and type 16 too. But always return -1. I don't know why.

I have try this too :

int posRain = System.Array.IndexOf (RainTime, day);

That's always return -1 too.

I have tested it at online C# Tester here : https://csharppad.com/

It works at it return 10.

But in unity c# editor it is different always return -1.

Could someone explain what is going on ?

Thanks

Dennis

4
  • how about trying simply x == 16 instead of x.equals(16)? Commented Feb 16, 2017 at 8:23
  • 3
    I haven't looked much at your code, but your array variable is public which makes unity serialize it in the inspector. Any value in the inspector to the field will override the code values you've set. So I'm guessing that this array, in the inspector, has no values or not the values you have in the code. the "-1" value means it doesn't exist in the list. Commented Feb 16, 2017 at 8:25
  • @PiotrKamoda it is same and return -1. Fredrik I guess your are a bit right in here. I have found the answer finally. It is not about serialize. But it is about to resetting the value in inspector. or reattach the script. So all array value size will update. I got this when you say inspector value. I check it and right value 16 is not in there. Commented Feb 16, 2017 at 8:32
  • Thanks for both of you. You help me a lot and figured the answer. :) Commented Feb 16, 2017 at 8:33

2 Answers 2

1

Your array is public, so it's serialized by Unity. Unity will set the array to the value you gave it in inspector, overriding the one you declared in the code. If you don't need to access the array from the inspector, you should use [NonSerialized] attribute. If you do need to access it from inspector, you should also edit it from there.

Sign up to request clarification or add additional context in comments.

Comments

0

Finally Got The ANSWER....

And finally this is a little thing we should remember. Sometimes this can make us onfused.

Once you update the array value at script. Don't forget to resetting or reattach the script that contain array value in the inspector. When you resetting or reattach the script all the value array size will update.

For example :

in script you have an array :

public int[] RainTime = new int[]{6,7,8,9,10,18,19,20,21,22};

This is not include 16 in array. Now you run the code in editor. In the inspector you will see that array size contain value of all RainTime array value.

Now back to the script update the array value at :

public int[] RainTime = new int[]{6,7,8,9,10,18,19,20,21,22,**16**};

You have put 16 at last array. Now Run the code in editor. And see in the inspector array size value, Value 16 is not contain at array in the inspector even you have update it via script.

What you should you is to Reset The value from inspector OR reattact the script from inspector. It will update the value and now array in the inspector now contain value 16 too.

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.