While working on one of my projects today I came across something very, very weird. I tried to declare a public array of 100 bools, but would for some reason get an array with a length of 0. I tested it further with a few other datatypes, but neither of them gave different results. I tried opening one of my other projects but nothing changed. I also tried setting the size from the inspector instead, but it would reset to 0 when starting the game. Finally, I started a new project and this time it went back to normal. I could declare arrays and I could change their size from the inspector. However, as soon as I go back to the other two projects the problem resurfaces.
It might be worth noting that all arrays that are already in the script act like normal, so as long as I don’t try to change something my scripts work just fine.
I’m still very much a novice programmer, but from the little I know it seems like the arrays don’t serialize. As soon as I select a new object the size is forgotten. But there’s absolutely no reason for that to happen. I’m working on a custom inspector for one of the projects, but the other one is completely vanilla and very, very simple.
This is the piece of code I used for testing, by either setting the length from the inspector or by adding “= new bool[100]”.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public bool Jump = true;
protected Animator animator;
public float Speed;
public float JumpHeight;
public ParticleSystem Particles1;
public ParticleSystem Particles2;
public bool [] Test = new bool[100];
void Update () {
print(Test.Length);
}
// Use this for initialization
void Start () {
animator = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
rigidbody2D.AddForce(Vector2.right * Input.GetAxis("Horizontal") * Speed);
if (Input.GetAxis("Horizontal")>0)
{
Vector2 temp = new Vector2(-1, transform.localScale.y);
transform.localScale = temp;
Particles1.enableEmission = false;
Particles2.enableEmission = true;
animator.SetBool("Walk", true);
}
if (Input.GetAxis("Horizontal")<0)
{
Vector2 temp = new Vector2(1, transform.localScale.y);
transform.localScale = temp;
Particles1.enableEmission = true;
Particles2.enableEmission = false;
animator.SetBool("Walk", true);
}
if (Input.GetAxis("Horizontal") == 0)
{
animator.SetBool("Walk", false);
}
if (Input.GetAxis("Fire1") > 0 && Jump == true)
{
rigidbody2D.AddForce(Vector2.up*JumpHeight);
Jump = false;
}
}
void OnCollisionEnter2D (Collision2D collision)
{
Jump = true;
}
}
I have restarted both my computer and software. All my code is written in C# using MonoDevelop. I’m on a Macbook Pro running Mavericks.