# Beginning C# - Part 17: Structs | Ray Wenderlich

**URL:** <https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525>\
**Category:** 🎓 Courses & Articles\
**Created:** [November 10, 2016, 9:03pm UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525 "2016-11-10T21:03:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vegetarianzombie](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vegetarianzombie/32/94022_2.png) [@vegetarianzombie](https://forums.kodeco.com/u/vegetarianzombie)\
**Post date:** [November 10, 2016, 9:03pm UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/1 "2016-11-10T21:03:50Z")

</div>

In this video, you learn your first object type, the struct. This enables you to group data and pass it around in your code.

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/3247-beginning-c/lessons/17](https://www.raywenderlich.com/3247-beginning-c/lessons/17)

---

<div class="post-metadata">

**Author:** ![mickbass](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mickbass/32/72062_2.png) [@mickbass](https://forums.kodeco.com/u/mickbass)\
**Post date:** [October 1, 2017, 12:08am UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/2 "2017-10-01T00:08:54Z")

</div>

Apparently i have an error but i don’t see where it is, It is only printing the last one then NULL for the rest

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;

struct BestBook{  
public string Name;  
public string Author;  
public float AvgRating;  
public int Score;  
}

public class BestBooks : MonoBehaviour {

```
BestBook[] books;

// Use this for initialization
void Start () {

	books = new BestBook[3];

	BestBook hungerGames = new BestBook ();
	hungerGames.Author = "Suzanne Collins";
	hungerGames.Name = "The Hunger Games";
	hungerGames.AvgRating = 4.35f;
	hungerGames.Score = 2664643;

	BestBook harryPotter = new BestBook ();
	hungerGames.Author = "JK Rowling";
	hungerGames.Name = "Harry Potter and The Order Of The Phoenix";
	hungerGames.AvgRating = 4.45f;
	hungerGames.Score = 2173146;

	BestBook mokingbird = new BestBook ();
	hungerGames.Author = "Harper Lee";
	hungerGames.Name = "To Kill A Mocking Bird";
	hungerGames.AvgRating = 4.25f;
	hungerGames.Score = 1797878;

	books [0] = hungerGames;
	books [1] = harryPotter;
	books [2] = mokingbird;
}

void OnDisable(){
	foreach(BestBook book in books){
		Debug.Log (book.Name);
		Debug.Log (book.Author);
		Debug.Log (book.AvgRating);
		Debug.Log (book.Score);
		Debug.Log ("----------");
	}
}

// Update is called once per frame
void Update () {
	
}

```

}

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [October 1, 2017, 12:25am UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/3 "2017-10-01T00:25:22Z")

</div>

This is happening because although you create other two books, you are assigning all your values to the hunger games book.

For example:

```
BestBook mokingbird = new BestBook ();
hungerGames.Author = "Harper Lee";

```

So you see, you’ve created a mockingbird book, but you are assigning values to the hungerGames book.

---

<div class="post-metadata">

**Author:** ![mickbass](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mickbass/32/72062_2.png) [@mickbass](https://forums.kodeco.com/u/mickbass)\
**Post date:** [October 1, 2017, 2:02am UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/5 "2017-10-01T02:02:57Z")

</div>

Oh yeah i see the error now… Thats what i get when i copy and paste 😄  
Thanks for the ultra fast reply Awesome… Great Tutorials also

---

<div class="post-metadata">

**Author:** ![clb3820](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@clb3820](https://forums.kodeco.com/u/clb3820)\
**Post date:** [January 29, 2018, 9:32pm UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/6 "2018-01-29T21:32:46Z")

</div>

Hello,

I’m getting the following error:

NullReferenceException: Object reference not set to an instance of an object StructDemo.OnDisable() (at Assets/Scripts/Beginning OOP/StructDemo.cs:44)

line 44 is foreach (BestBook book in books) {

My Code is:

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;

struct BestBook {  
public string Name;  
public string Author;  
public float AvgRating;  
public int Score;  
}

public class StructDemo : MonoBehaviour {

```
BestBook[] books;

void Start () {

	books = new BestBook[3];

	BestBook hungerGames = new BestBook ();
	hungerGames.Author = "Suzanne Collins";
	hungerGames.Name = "The Hunger Games";
	hungerGames.AvgRating = 4.35f;
	hungerGames.Score = 2664643;

	BestBook harryPotter = new BestBook ();
	harryPotter.Author = "JK Rowling";
	harryPotter.Name = "Harry Potter and The Order Of The Phoenix";
	harryPotter.AvgRating = 4.45f;
	harryPotter.Score = 2173146;

	BestBook mockingbird= new BestBook ();
	mockingbird.Author = "Harper Lee";
	mockingbird.Name = "To Kill A Mocking Bird";
	mockingbird.AvgRating = 4.25f;
	mockingbird.Score = 1797878;

	books [0] = hungerGames;
	books [1] = harryPotter;
	books [2] = mockingbird;
}

void OnDisable() {
	foreach (BestBook book in books) {
		Debug.Log(book.Name);
		Debug.Log(book.Author);
		Debug.Log(book.AvgRating);
		Debug.Log(book.Score);
		Debug.Log("---");
	}

}

```

}

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [January 30, 2018, 2:56am UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/7 "2018-01-30T02:56:05Z")

</div>

@vegetarianzombie Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![clb3820](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@clb3820](https://forums.kodeco.com/u/clb3820)\
**Post date:** [January 30, 2018, 8:34pm UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/8 "2018-01-30T20:34:38Z")

</div>

Booted up Unity today and the error disappeared. Thanks guys.

---

<div class="post-metadata">

**Author:** ![aslansjoy](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@aslansjoy](https://forums.kodeco.com/u/aslansjoy)\
**Post date:** [July 16, 2018, 3:43am UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/9 "2018-07-16T03:43:25Z")

</div>

It bears mentioning that structs need to be declared outside the class, or at least that’s the way these scripts are working.

---

<div class="post-metadata">

**Author:** ![rafaelriva](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rafaelriva/32/83387_2.png) [@rafaelriva](https://forums.kodeco.com/u/rafaelriva)\
**Post date:** [November 2, 2018, 8:34pm UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/10 "2018-11-02T20:34:34Z")

</div>

Hey @vegetarianzombie, this is definitely getting harder.

I tried the challenge and got stuck in printing the String Array.

Here is my code:

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

struct MyFavoriteMovies
{
    public string GenreType;
    public int StarRating;
    public string[] MovieNames;
}

public class favoriteMovies : MonoBehaviour {

    public string genreType;
    public int starRating;
    public string[] movieNames;

    MyFavoriteMovies myMovies;

	// Use this for initialization
	void OnDisable () {
        MyFavoriteMovies myMovies = new MyFavoriteMovies();
        myMovies.GenreType = genreType;
        myMovies.StarRating = starRating;
        myMovies.MovieNames = movieNames;
        Debug.Log(myMovies.GenreType);
        Debug.Log(myMovies.StarRating);
        foreach (string movieName in myMovies.MovieNames)
        {
            Debug.Log(movieNames);
        }
    }
}

```

the foreach loop isn’t working as aspected and i don’t know why. ☹

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 2, 2018, 9:40pm UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/11 "2018-11-02T21:40:56Z")

</div>

@rafaelriva You should replace `movieNames` with `movieName` inside the `foreach` loop to print out each movie name:

```
foreach (string movieName in myMovies.MovieNames) {
  Debug.Log(movieName);
}
```

Please let me know if you have any other questions or issues about the whole thing. Thank you!

---

<div class="post-metadata">

**Author:** ![rafaelriva](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rafaelriva/32/83387_2.png) [@rafaelriva](https://forums.kodeco.com/u/rafaelriva)\
**Post date:** [November 3, 2018, 3:20pm UTC](https://forums.kodeco.com/t/beginning-c-part-17-structs-ray-wenderlich/22525/12 "2018-11-03T15:20:59Z")

</div>

Thanks! i got it now! 😄

I had decided to start from scratch and also rewatching the video again, i managed to make an more complex script now!

Thanks 😃
