Stored data and GameObject

Hello everybody,

I’m working with Unity for a couple of months and I am actually facing a “problem”. It’s more a question than a problem but I would like to know how works the management of data.

Here is my point with an example: I would like to create an instance of the class Cat.

Cat is a simple C# class with functions and attributes.

public class Cat : ISerializable{

   private string name;
   public string Name{
      get{return name;}
      set{name = value;}
   }

   private int age;
   public int Age{
      get{return age;}
      set{age = value;}
   }

   ...

   public Cat(string name, int age){
       this.name = name;
       this.age = age;
   }

   ...
}

I instantiate my new cat Billybob with a little editor by giving the name and the age of the newborn.

Until there, nothing is special. My question is : Am i allowed to get my Billybob information without putting the instance Billybob in a MonoBehaviour attached to a GameObject in my Scene?

Or am I forced to made my Cat class inherit from MonoBehaviour and to create a GameObject Billybob and do an AddComponent(“Cat”) stuff?

The simple question is : do I need GameObject to access data that i’ve created :)?

Thanks for any help.

Iwa

1 Answer

1

No you don’t. You just need to make the instances of it somewhere. That could be inside a MonoBehaviour or a static class.

Thanks for your answer. I still wonder how do i get access to my information of Billybob if it is a static class? And what will my static class will look like?

public static class SomeClass { public static List<Cat> cats = new List<Cat>(); } Then to access it: SomeClass.cats.Add(new Cat("Billybob", 2));

Okay thanks a lot for the explanation :)