0

I have a model with an animator; and a controller script to make it move, then I created a simple class called "TheEntity", which hold a name, and an int for the energy. When the time goes by, the energy goes down, so the mesh walk around or perform animations at random.

public class TheEntity()
{
    public string name;
    public int energy;

    public TheEntity()
    {
        // make a random name
        name = "joe"+ rnd.next(1,1000).toString();
        energy = rnd.next(20, 100);
    }

}

When the energy value goes to 0, the mesh "goes to sleep", and regenerate again in a certain amount of time.

If I have a list of TheEntity instances, as it would be if I have a list of NPC, what would be the correct way to assign a mesh to each entity class in the list?

Should I have a script on the mesh, that has a reference to a TheEntity class. and assign it at runtime when I load the mesh prefab? Or should I put the whole "TheEntity" class script on the mesh, and save it as prefab, so every time that I load the prefab, I will have a mesh with a related TheEntity instance directly?

1 Answers1

1
  1. "Things" in the Unity scene are GameObject , that's all there is to it.

  2. Your class must be a MonoBehaviour to be on a game object.

  3. In Unity, everything is a MonoBehaviour (a Component). There is, quite literally, nothing else whatsoever in Unity.

(Of course, you may have some "raw" non-Unity classes for things like say math calculations, but that's irrelevant.)

It's just that simple.

public Class Entity:Monobehaviour
{
}

attach that to an empty game object. Add the models (meshes) .. or whatever you want. Add sound effects, add anything.

Regarding "changing the mesh", no problem.

Do that in a routine in Entity, if you like.

public Class Entity:Monobehaviour
{
public void ChooseRandomMesh()
{
}
public void ChooseRandomColorPattern()
{
}
public void RunForTheHills()
{
}
public void AttackHero()
{
}
}

If you prefer, write a Component which does nothing other than randomly change the mesh.

public Class Entity:Monobehaviour
{
public void RandomizeEntity()
{
}
public void ChooseRandomColorPattern()
{
}
}

.. and attach that script to the game object, also.

In Unity, everything is a MonoBehaviour (a Component), it's that simple.

Regarding making it a prefab, if you want to, sure do that. Read any of thousands of tutorials on prefabs.


There's a critical comment you made in your comments below:

"Also the entity class is not MonoBehaviour..."

Here's an incredibly critical point in understanding Unity:

1) You're quite right that your "model" or "AI" or "database connection" sort of has "nothing to do" with unity game objects. (They of course don't have a "position" or "mass!" or anything, right?!)

HOWEVER HOWEVER HOWEVER HOWEVER HOWEVER HOWEVER HOWEVER HOWEVER HOWEVER

2) In Unity unless a class is a MonoBehaviour: you can't do anything with it/ You can't even access the run loop, it's a total nonstarter.

THUS THUS THUS THUS THUS THUS THUS THUS THUS THUS THUS THUS THUS THUS

3) In Unity all the stuff like that, IS IN FACT a MonoBehaviour AND YOU SIMPLY sit it on an empty game object. (Usually the name in the Heirarchy starts with underscore, say, so you know it's "not really" a conventional game object.)

The simple bottom line is in your preload scene (you must have one in 100% of projects) you have all your "AI" and "model" and "database" stuff, just sitting on one or more "pretend" markers (marker == otherwise empty game object).

Bottom line, when you say below "your model is not a MonoBehaviour" that is wrong. It will have to be a MonoBehaviour (if you think about it, it's absolutely inevitable you'll need to access the runloop, if nothing else) ... just make it a MonoBehaviour and put it on a marker, almost certainly in your preload.

I hope it makes sense. Essay on the topic.

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Thanks Joe. I am using a MVC paradigm for my application; the entity for the player is actually the model, while the gamemanager is the controller. My issue was to associate the 2 at runtime, and since most of the training videos show scripts attached to ai prefabs, I thought that there may be a different way to do so. –  Mar 01 '16 at 22:24
  • Also the entity class is not monobehavior, and you did mention that you can only attach scripts that are derived from monobehavior, on a game object; so I have to connect it via a different script that will go on the mesh gameobject, either by reference or other means –  Mar 01 '16 at 22:26
  • Hi Newb. that's not what a "model" is in MVC. the "model" is basically your data. pls explain in 3 words what your game is. ie, is it FPS, side scroller, puzzle or what. cheers – Fattie Mar 01 '16 at 23:22
  • MVC is very confusing in an ECS system. don't forget ALL of unity, the entire paraphernalia of animations, physics, rendering etc ... ALL of that is just "View". only a handful of people understand MVC in Unity. anyway you're on track enjoy! – Fattie Mar 01 '16 at 23:25
  • Long essay for you .. .please vote it up :) http://stackoverflow.com/a/35465978/294884 – Fattie Mar 01 '16 at 23:27
  • Thanks a lot for the details :! –  Mar 02 '16 at 02:26
  • BTW to asnwer the previous question: my game is a sport managing game: you have players (free agents or in a team), you buy and sell them, you use them in a match and so on. The entitites are the players; and this is why I did design the game in the way it is now. I find really hard to wrap my head around Unity "freedom"; it is fundamentally like working with multiple inheritance in C++, and things get out of hands fast! So I did try to set up a MVC, where the entity is the model, the controller is the game manager and the view is half the UI, half the mesh that perform the animations :) –  Mar 02 '16 at 02:46
  • 1
    Right, unity is ECS (not OO), it is very weird. (wikipedia ECS) What you said is TOTALLY CORRECT. when we do sports dotcoms, you would have scripts that handle game logic (players, buying, scores- whatever). all of those would have NOTHING TO DO WITH graphics, just the logic of play. >>>HOWEVER<<< the scripts would literally be MonoBehaviour. You would put them on a marker object in your preload scene. The only other issue is accessing them with "Grid" syntactic candy. http://stackoverflow.com/a/35524924/294884 there are no macros or preprocessor in c# so use something trivial like that – Fattie Mar 02 '16 at 03:15