Keeping track of all my copies

Hello,

I'm a hobby game maker moving from C++ to Unity in an effort to get beyond stick-figure graphics. I've been having trouble figuring out how to 'copy' my old, very basic, programs, which I would like to do before moving on to scary new things. I'd be happy for any references to objects that behave 'like' the C++ things below - not much luck so far.

Normally, I would have some classes representing game objects (say a Penguin class and a Monkey class), all bundled into a small number of 'gamestate' objects that have pointers to everything represented on the screen. Throughout the game, Penguins and Monkeys would spawn and die and otherwise do their thing, but I would have one master list of pointers to all Penguins, and another list of pointers to all Elephants, which I could easily iterate through, find objects in (e.g. upon a mouseclick event), and otherwise act on. Without things like parameterized classes (to deal with Penguins and Monkeys in a unified way) or easy ways to save lists of pointers (to create the big list).

Maybe to make this slightly concrete, I'm interested in making a Monkey prefab, so that every monkey instantiation runs around picking up bananas independently (I don't care so much about the running around code). How could I find the particular monkey at a particular time so that (Number of Bananas)/(Distance to User) is maximized?

Thanks for your patience in reading through this. Any advice on how to ask questions here is also welcome - I've gone through some tutorial games, but none seem to be very helpful with these sorts of 'structural' questions.

1 Answer

1

Small example of instantiating a monkey prefab using a Monkey script reference, then storing it:

using UnityEngine;
using System.Collections.Generic;

public class MonkeySpawner
{
    private List<Monkey> monkeys = new List<Monkey>();
    public Monkey prefab;

    void Start()
    {
        for (int i = 0; i < 10; ++i)
        {
            monkeys.Add(Instantiate(prefab) as Monkey);
        }
    }
}

In the above, I'm treating the script reference on the prefab itself as the prefab, so that when I instaniate it, I get a Monkey script reference back, which I can add into a List.

This is pretty much identical to your parameterized Monkey class, and putting the pointer into a list.

As long as you don't treat gameobjects as the class you're interested in, and instead use the script components you place on them as being the class itself, then it's fairly easy - the script itself has access to the GameObject via the gameObject parameter, as well as every other component on the gameobject (GetComponent or the helper properties)

Thanks! So, I can have this script floating unattached, then put a static MonkeySpawner attached to a GameObject and refer to that all over the place? In any case, it certainly looks the same. Accepting answer.

The script would still be attached to a gameobject - you'd just drag the entire prefab onto the prefab slot in the inspector and it'll act as if it were a monkey script with other stuff attached.