Search Unity

string[] C# problem

Discussion in 'Editor & General Support' started by elias723, Jul 30, 2006.

  1. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    Hey everyone. I need to make a C# script that has an array of strings that I can modify in the editor. I have this right now:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class debugaxes : MonoBehaviour {
    6.     public string[] axes;
    7.     private string data = "Debug Axes:\n\n";
    8.    
    9.     void Update()
    10.     {
    11.         data = "Debug Axes:\n\n";
    12.         for(int i = 0; i < axes.Length; i++)
    13.         {
    14.             data += axes[i] + " : " + Input.GetAxis(axes[i]) + "\n";
    15.         }
    16.         guiText.text = data;
    17.     }
    18. }
    19.  
    When I try to put in the Size of axes in the inspector, it changes it back to zero, but I don't know why. Anybody have a solution or workaround?
     
  2. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    Maybe you could try initializing the array? i.e.:
    Code (csharp):
    1. public string[] axes = new string[];
    That's just a guess, not sure if it works.
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Seems to be a bug in the property inspector.
    You can work around it by making an non-empty string array by default:

    public string[] axes = new string[1];
     
  4. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    I still haven't been able to get a public string array working in either js or c#, even with the suggestions you guys have. The only solution I could find is using a private array instead, which works for me since I won't be changing the values much until I move the script to other projects.
     
  5. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    I've run into the same problem as you now, and you're right, there doesn't appear to be any workaround. Can we expect this to be fixed in 1.5.1? Or is there another way that one could get around this bug in the short term?
     
  6. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Code (csharp):
    1. public string[] yo = {" "};
    That kicks the inspector into working.

    -Jon
     
  7. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    Awesome, thanks aarku!
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    On top of that, string resizing will be allowed when there are zero elements with Unity 1.5.1.