#@Fen Ants crawling around droping

1 messages · Page 1 of 1 (latest)

quasi wedge
#

let's do a thread; it's a bit noisy

#

okay, so are ants needing to load many different kinds of Pheromone prefabs?

#

rather than just using a single prefab that gets modified?

novel jay
#

Gladly 😄

#

Thanks for your help

#

For now I'm just using one kind of prefab

quasi wedge
#

Is there a reason you can't just have the ant reference the prefab asset directly?

#

That could mean making the ant prefab reference the pheromone prefab, or it could mean having your ant spawner assign the ant a pheromone prefab

novel jay
#

How could i make the Ant prefab reference the pheromone prefab directly?

quasi wedge
#

just give the ant a serialized Pheromone field and drag it in

#

i.e. either public Pheromone pheromone or [SerializeField] Pheromone pheromone

#

the latter is "better", nominally, since it doesn't add a public field to the ant

#

the pheromone is the ant's business, not the business of every single class in the game

novel jay
#

I guess I'll have the same problem. The C# file does not know about "Pheromone", so i can't create a field of that type

quasi wedge
#

Why?

#

Is there not a Pheromone class?

novel jay
#

I had a Pheromone class (the mono behavior for the Pheromone), so Prefab and class both had the same name. Then Recources.Load would not properly load the Prefab. My guess was, because it was trying to load the class "Pheromone", but found the prefab "Pheromone". Thats where my problem started

quasi wedge
#

The name should not matter.

#

Resources.Load will look for an asset with the given name in any resources folder

#

It will try to load the asset as the given type

novel jay
#

Currently i renamed the class to PheromoneScript, which i don't like and should probably not be necessary

quasi wedge
#

You can keep it as Pheromone

quasi wedge
#

/Assets/Resources/Pheromone.asset

novel jay
#

Give me a sec, I'll build it back

quasi wedge
#

Resources.Load<GameObject>("Pheromone") would load that prefab asset as a GameObject

#

Resources.Load<Pheromone>("Pheromone") would load that prefab asset as a Pheromone

novel jay
#

What the heck, now it is indeed working as intended

#

I think the only difference is, now I'm using Resources.Load<Pheromone>("Pheromone"), and before i used Resources.Load("Pheromone") as Pheromone, which now still returns "null"

quasi wedge
#

Ah, yeah, I don't know what happens if you omit the generic type parameter.

#

well, it should have at least returned a UnityEngine.Object

novel jay
#

just null and I've no idea how to debug that, but I guess Resources.Load<Pheromone>("Pheromone") is the proper way anyway

#

Thanks alot for your help!

quasi wedge
#

I only use Resources when I have no reasonable way to get the reference

#

in this case, you should just reference the prefab directly

novel jay
#

I'm new to game engines and don't realy like to way of dragging stuff into fields in the inspector. Maybe thats just something I have to get over with, or do you know of another way to get a direct reference?

quasi wedge
#

Dragging stuff around is good! It means that you can rename assets without breakage

#

It also means that Unity can tell which assets you do and don't use

#

Everything in a Resources folder is automatically included in a build

#

because it can't decide if an asset is or isn't used

novel jay
#

I get it. Actually i dislike referencing stuff by their names as Strings even more tbh, so i guess I'll change that 😉

quasi wedge
#

Yeah, strings are brittle

#

I use Resources for a "singleton scriptable object" type

#
using UnityEngine;

public abstract class SingletonSO<T> : ScriptableObject where T : SingletonSO<T>
{
    private static T _instance;
    public static T Instance 
    {
        get
        {
            if (_instance == null)
            {
                _instance = Resources.Load<T>(typeof(T).Name);
            }

            return _instance;
        }
    }
}
#

since there's no real way to assign a reference for something that's going to be static

novel jay
#

I'll try to keep that in mind for when i get to a related problem

quasi wedge
#

Resources.Load is a great way to break the "turtles all the way down" problem

#

to access an asset without a reference you set in advance

#

one of my games has a settings system based on Scriptable Objects

#

I have categories that reference other categories, along with specific settings

#

The top-level category is referenced by a special singleton

novel jay
#

I got rid of the Load function and it works with a serializeField now. Thanks again!