About 'Creating An Array'!

In this video (objectives)…

  1. What is an array and how are arrays created.
  2. Returning an array type variable from within a public method.
  3. Assigning array elements in Unity by dragging and dropping into the inspector.
  4. How to lock the inspector window.

After watching (learning outcomes)…

Understand how to create arrays and assign elements to arrays in code or the inspector.

(Unique Video Reference: 9_TX_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

I’ve been having an issue where my Visual Studio doesn’t autocomplete like yours, does not show all errors, and does not change the color of certain words like yours. For instance where I have State state written the first State is blue in your example but in mine it remains black. could you please assist me with this?

Hi there, often this requires a restart of Unity and VS. Also, check that you are double clicking your file from within Unity to open it. If you’re still having trouble, here is a forum discussion on stackoverflow with some ideas:

Hi !
Sorry for the very beginner-like question, but… I am a true beginner actually.
I am struggling on some parts, and here, I just can’t get it right (no problem with the code, only with the understanding).

Why did we have to write at first

public string GetStateStory
But then

public State GetNextStates()

I don’t understand why at first it was “string”, and then it was “State”. I still don’t get why some keyword must be used instead of another. I guess it has something to do with one being a method and the other a type, but
that doesn’t really help me (because I don’t understand when it is supposed to be a method or a type or anything else) .Hope you can clarify this. And thanks a lot for your lessons, they’re really nice to follow.

1 Like

I am new in this too, but I will try to answer it. You have two methods, GetStateStory() and GetNextStates(). The first, when called, will send the story text in the scriptable object (a string), and the second will send back the array of states in that same scriptable object.

It’s like a file cabinet full of file folders. A folder is a state. In every folder there is a paper with the story and a stack of cards. In every card there is a link to a different folder (the states where this state can go). When you call GetStateStory(), it takes the paper with the story of this folder, and send what is write in it (words, so a string). When you call GetNextStates() you are asking for the links, so it sends back the stack of cards, a list of the states you can go to, from the folder (the state) where you are, so it returns an array (list) of states (Sates).

1 Like

Thanks a lot for your answer! I think it’s getting clearer now.
So, if I got it correctly:

The string (type?) is used here because we want the words from the story text to be ultimately displayed on the scriptable object through the GetStateStory method (it won’t be display with just this method, but we needed first to send the data).
The State type is used here because we want to move from a state to another state through the GetNextStates method. However, this alone will not send the story text, this type only makes us move from a state to others, hence the use of State and not string. I assume, then, that this can only be used thanks to the former GetStateStory method and its string type. Without it, we would say to the program “look at those states”, but not what to do with them.

I have doubts however about the string and scriptable object, I have the feeling in didn’t get those definitions right.

Anyway, thanks a lot again!

Coming from the older version of the course, I really like how we are developing Text101 now.
I was dreading having to do the all the game in-code.

Keep up the good work guys. :+1:

1 Like

Hey there! New student of C# here. I was wondering why that we don’t have after nextStates (i.e. nextStates) here: Capture

I’m used to programming in Arduino so I think that’s just a difference I’ll have to adjust to.

Still a bit confused as to how the whole thing works. I haven’t really practiced up much with return types so its the first part of the course that’s been egging me. I’m following along fine but I really want to understand these more since they seem useful.

I had this issue as well. I had to manually install the unity package for VS by going to ‘Tools’, then ‘Get tools and features’ then searching for or scrolling to find ‘Game development with Unity’

Can someone explain to me why each new scriptable object created gets a copy of the storyText variable that was created in the State class?

Hello!
First off, I’m making great progress in this course and learning a lot so I wanted to thank you for that! I have two questions that aren’t necessary to the project but they do relate to something that was covered in this lecture.

Right now, when the user enters a number, we are manually mapping each number to a specific position in the states array. (For example, if the user presses 1, they will always go to the 0th index of the states array).

My first question is this: Is there a way to parse the user’s input as an integer and use the integer to access the array instead?

I’m asking this because sometimes I have a state where there is only one option. If the user enters “2”, that number is still mapped to index 1 of the states array, but that position is out of bounds (an error message appears on the console but the game doesn’t break). What I would like to do is store the user’s input as an integer, test if the input is less than the length of the array, and if so, then move on to the next state. Otherwise, maybe provide a quick feedback message to the user to let them know that button is invalid.

That brings me to my second question: How do I access the length of the array in C#?

I know these questions aren’t necessary to the game’s functionality, but I’m a bit of a perfectionist and have a few computer science courses under my belt. Any information would be greatly appreciated! For now, I’ll move on with the material!

Thank you again and I hope that an answer to this question can be of some use to someone else as well!

I guess that by this time you already answered your question but…

You write [] besides State because you are declaring an array. Variables are always plain names referring to types. In this case we have nextStates being an array. What kind of array? “Array of State(s)”. It is an array that contains instances of State.

1 Like

Hello Bazerous,

First of all, the easy answer.

To get the length of an array in C# you have to write, for example:

Debug.Log(nameOfTheArray.Length);

nameOfTheArray.Length returns an int with the number of elements contained in the array.

About your first question… I haven’t finished all the lessons in this part but… Don’t you need to have different arrays in every state so you can fill 'em whit the choices you want the player to take?

1 Like

I am facing this issue. The size heading does not appear. Help me!

Looks like you might need to share your code with us so we can look to see what the problem might be.

Privacy & Terms