#@Fen Ants crawling around droping
1 messages · Page 1 of 1 (latest)
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?
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
How could i make the Ant prefab reference the pheromone prefab directly?
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
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
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
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
Currently i renamed the class to PheromoneScript, which i don't like and should probably not be necessary
You can keep it as Pheromone
so, supposing you had a file named
/Assets/Resources/Pheromone.asset
Give me a sec, I'll build it back
Resources.Load<GameObject>("Pheromone") would load that prefab asset as a GameObject
Resources.Load<Pheromone>("Pheromone") would load that prefab asset as a Pheromone
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"
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
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!
I only use Resources when I have no reasonable way to get the reference
in this case, you should just reference the prefab directly
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?
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
I get it. Actually i dislike referencing stuff by their names as Strings even more tbh, so i guess I'll change that 😉
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
I'll try to keep that in mind for when i get to a related problem
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
I got rid of the Load function and it works with a serializeField now. Thanks again!