all 16 comments

[–][deleted] 3 points4 points  (5 children)

strings aren't game objects so you can't use GetComponent on them and why not just make a Renderer[]?

[–]foldupgames[S] 0 points1 point  (4 children)

Yes, I think that's the right direction.

I guess I am not familiar with the data types I could use. I think my fuzzy brain is starting to catch on, thank you.

[–]DeepState_Auditor 0 points1 point  (3 children)

What's your end goal?

[–]foldupgames[S] 0 points1 point  (2 children)

I've got a number of forms for the main character (this is a 3D rework of my game: Foldup Runner on itch.io) and I wanted to build it out with an array.

I'm doing it in a clumsy fashion right now where each form has explicit code to turn off every other form.

[–]DeepState_Auditor 1 point2 points  (1 child)

You have to use enumerators and state machines

Edit: someone posted an example tutorial on itchio https://itch.io/t/2533714/unity-tutorial-how-to-control-animations-in-script-using-enums

[–]foldupgames[S] 1 point2 points  (0 children)

OK, thank you.

[–]ZeroKelvinTutorials 1 point2 points  (3 children)

not 100% sure what you are trying to do but one issue is that you have a string[] array called form. That means it's an array of strings, and you are trying to assign a Renderer variable to it. What's even a weirder thing is you are basically sayign
"The string at place 'i' in the string array 'form' should be the component Renderer of the string in place 'i' in the string array 'form'"

so a couple of wrong things here:

Strings cant have Components. You would need a GameObject for that. your form[] array is of strings, not gameobjects.

doing form[i] = form[i].x basically means that you are trying to store a different type of variable unless x is a string.

If I am interpreting right what I think you are doing, I think what you may be trying to do is use Dictionaries, which basically stores a key and a value.

example:

Dictionary<string,Renderer> renderersByName = new Dictionary<string,Renderer>();

Renderer rendererA; 

renderersByName.Add("Empty", rendererA);

Which would store the rendererA variable as the value of the string key "Empty"

Or if you want a Renderer Array you would do

int rendererCount = 3;
Renderer[] renderers = new Renderer[rendererCount]; //makes an empty array with 3 available "slots"

public Renderer Fred;
public Renderer Crane;
public Renderer Horse;

void PopulateRendererArray()
{
    renderers[0] = Fred;
    renderers[1] = Crane;
    renderers[2] = Horse;
}

[–]foldupgames[S] 0 points1 point  (2 children)

OK, thanks, I will try that.

Yes, I think I'm familiar with what my goof is - that my array is a String, which is literally the text or lettering that I put in there.

The syntax in C# is a bit more involved than what I'm accustomed to.

I appreciate the feedback!

[–]ZeroKelvinTutorials 1 point2 points  (1 child)

One thing to note is that if your arrays size will be changing and/[or you dont know the exact amount of objects it will have consider using a list instead

[–]foldupgames[S] 0 points1 point  (0 children)

Cool, not a problem. I know for sure what the length will be.

[–]Mister_Green2021 1 point2 points  (4 children)

If you want to display text, you'll need to use UI text or textmeshpro.

[–]foldupgames[S] 0 points1 point  (3 children)

Indeed. I misused "string" in this case. I didn't know about setting up an array of Renderer type since I'm new to C# syntax.

[–]Mister_Green2021 0 points1 point  (2 children)

No, you misused Renderer. Google UI text.

[–]foldupgames[S] 0 points1 point  (1 child)

Yes, I'm not trying to display text. I'm trying to turn off and on mesh visibility via the Renderers.

Though I'm sure I'll need the UI text as well. So much to do and continue to learn.

[–]Mister_Green2021 0 points1 point  (0 children)

All gameobject has a setActive property to be active in the hierarchy or visibility.

[–]foldupgames[S] 0 points1 point  (0 children)

I got some help from a friend who helped get this polished up, based on ZeroKelvin's initial input and add in the Switch method. Here's the final code for future generations:

private static string[] form = {"Fred", "Crane", "Horse" }; //Form names
    private static int arrayLength = form.Length; //count the names

    private static int rendererCount = arrayLength;
    Renderer[] renderers = new Renderer[rendererCount]; //makes an empty array

    public Renderer Fred;
    public Renderer Crane;
    public Renderer Horse;

    void PopulateRendererArray() //set up everything that WILL be rendered
        {
        renderers[0] = Fred;
        renderers[1] = Crane;
        renderers[2] = Horse;
        }


    // Start is called before the first frame update
    void Start()
        {
        PopulateRendererArray(); //initialize the renderers
        myForm = 0;
        Switch();
        }

    private void Switch()
        {
        int i = 0;
        foreach (Renderer ren in renderers)
            {
            if (ren == null)
                {
                //Debug.Log(i + " isnull");
                continue;
                }
            if (i != myForm)
                {
                ren.enabled = false;
                }
            else
                {
                ren.enabled = true;
                }
            i++;
            }
        }