Hi Guys,
I am having an Issue with three classes I have Just defined.
The first class “ObjectAndSound” contains 3 items 3 getter-function to return the values. The second class “AllItems” uses the first class to create Objects of type “ObjectAndSound” and stores them in a two-dimensional array(itemArray).The problem comes in when I call a third class “Test_AllItems”. When I call newItems.printAllItems(); it does not return the name of the game object.The array seems to store only empty GameObjects and I can’t figure out why. The console says: Name of GameObject is: .
I have attached the script to an empty GameObject
and did attach all the GameObjects and sounds to the Variables in the Inspector.
What might be the problem, that the names of the GameObjects are not returned?
Thanks a lot in advance!!
namespace ObjectSound
{
public class ObjectAndSound : MonoBehaviour {
private GameObject gameObject1;
private GameObject gameObject2;
private AudioClip audioClip;
public ObjectAndSound()
{}
public ObjectAndSound(GameObject obj1, GameObject obj2, AudioClip clip)
{
Debug.Log("Game Object was created");
this.gameObject1 = obj1;
this.gameObject2 = obj2;
this.audioClip = clip;
}
public GameObject returnFirstItem()
{
Debug.Log ("Name of GameObject is:" + this.gameObject1);
return this.gameObject1;
}
public GameObject returnSecondItem()
{
Debug.Log ("Name of GameObject is:" + this.gameObject2);
return this.gameObject2;
}
public AudioClip returnClip()
{
Debug.Log ("Name of Audioclip is:" + this.audioClip);
return this.audioClip;
}
}
namespace Items
{
public class AllItems : MonoBehaviour {
public AudioClip audioLampBottle;
public AudioClip audioLampBook;
public AudioClip audioLampCup;
public GameObject bottle;
public GameObject cup;
public GameObject lamp;
public GameObject book;
private ObjectAndSound[,] itemArray=new ObjectAndSound[2,3];
public AllItems()
{
//Create Objects of type ObjectAndSound
ObjectAndSound newObject0 = new ObjectAndSound (bottle,book, audioBottleBook);
ObjectAndSound newObject1 = new ObjectAndSound (bottle,cup, audioBottleCup);
ObjectAndSound newObject2 = new ObjectAndSound (bottle,lamp, audioBottleLamp);
ObjectAndSound newObject3 = new ObjectAndSound (book,bottle, audioBookBottle);
ObjectAndSound newObject4 = new ObjectAndSound (book,cup, audioBookCup);
ObjectAndSound newObject5 = new ObjectAndSound (book,lamp, audioBookLamp);
itemArray [0, 0] = newObject0;
itemArray [0, 1] = newObject1;
itemArray [0, 2] = newObject2;
itemArray [1, 0] = newObject3;
itemArray [1, 1] = newObject4;
itemArray [1, 2] = newObject5;
}
public void printAllItems()
{
for (int i=0; i<itemArray.GetLength(0); i++) {
for (int k=0; k<itemArray.GetLength(1); k++) {
Debug.Log ("Printing Data Information");
this.itemArray [i, k].returnFirstItem ();
this.itemArray [i, k].returnSecondItem ();
}
}
}
using Items;
public class Test_AllItems : MonoBehaviour {
void Start ()
{
AllItems newItems = new AllItems();
newItems.printAllItems ();
}
}