Hi there,
I’m learning about classes and constructors in Unity and I’m wondering how to go about outputting the details within a class to the console?
I’ve written two scripts. One is called Books.cs that has two constructors in it(one empty and the other has arguments passed through it) and the other is called Player.cs which is attached to a game object.
The code in Books.cs is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Books
{
public int id;
public string title;
public string author;
public Books(){
}
public Books(int id, string title, string author){
this.id = id;
this.title = title;
this.author = author;
}
}
The Player.cs code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Books books_1 = new Books();
Books books_2 = new Books(1,"Goosebumps","R.L Stine");
Debug.Log(books_1);
Debug.Log(books_2);
}
}
As you can see I’ve tried to Debug.Log the two instances I’ve created in the Player class but the consoles just outputs “Books” twice.
Do I have to use an array or something to output what shown in the second books_2 variable or something?