Beginning C# - Part 17: Structs | Ray Wenderlich

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

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 () {
	
}

}

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.

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

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!

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

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

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

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. :frowning:

@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!

1 Like

Thanks! i got it now! :smile:

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

Thanks :smiley: