#so i have to get the script from the
1 messages · Page 1 of 1 (latest)
thread
You need get a component from the game object
I don't like using the term "script"
it's kind of vague
the .cs files are scripts, but those scripts aren't literally attached to the objects in your scene
var oreScript = _ore.GetComponent<BasicOreScript>();
This will retrieve the component from the object you hit
you can then access the _worth field from it
It doesn't really matter how many ore objects there are in your world
The method will be called each time you collide with something
If 100 pieces of ore crash into you, you'll get 100 messages
ah
Now, there ARE places where you might need to handle many pieces of ore
Imagine if you had a component that tracked all of the ore in the world
It would need a List<BasicOreScript> field in it
public class OreTracker : MonoBehaviour {
public List<BasicOreScript> ores;
}
you could then put lots of pieces of ore into that list
Here, though, the method is always telling you about a single collision
Ah, but maybe you're talking about multiple kinds of ore
yeah lol
BasicOre and WeirdOre and SpecialOre
There are two options here.
One: you handle each one totally differently
void OnCollisionEnter(Collision collision) {
var obj = collision.gameObject;
if (obj.TryGetComponent(out BasicOre basicOre)) {
// do basic ore stuff
} else if (obj.TryGetComponent(out WeirdOre weirdOre)) {
// do weird ore stuff
}
}
TryGetComponent is useful for this kind of thing. It returns true if it found the component
and the component gets stored into that basicOre / weirdOre variable
This is what you'd do for completely different kinds of things, where there's no common behavior at all
so i'd make a if statement for every type of ore?
Right.
Alternatively, you might have some shared behavior
Maybe every kind of ore has a value
In that case, you'd do a little inheritance
public abstract class Ore : MonoBehaviour {
public float worth;
}
public class BasicOre : Ore { }
public class WeirdOre : Ore { }
Then you could do this
if (obj.TryGetComponent(out Ore ore)) {
// do stuff that works for any ore here
}
notice how BasicOre and WeirdOre don't actually have any code in them
It wouldn't really make sense to have entire classes for each kind of ore if they only differ in value
you can just change the number on each ore prefab you make
But suppose they're a little more complicated. You might do this...
public abstract class Ore : MonoBehaviour {
public float worth;
public virtual void Collect() { // do nothing }
}
public class BasicOre : Ore { }
public class WeirdOre : Ore {
public override void Collect() {
base.Collect();
GameController.weirdness += 1000;
}
}
If we have an Ore, we can call ore.Collect();
and the appropriate method will be called
if (obj.TryGetComponent(out Ore ore)) {
money += ore.worth;
ore.Collect();
}
If we hit a BasicOre, we just get some money
if we hit a WeirdOre, we get some money, and then its Collect method, uh...increases the weirdness
i see
This is called polymorphism
(i do not see)
All we care about is that we have an Ore
It's a lot to throw at you all at once, haha
Can you tell me more about how your game will work?
That might help.
basically a type of factory processing game where the player can place miners which drop ores onto these conveyors which can then be processed to increase their value
and use that money to expand
and im trying to figure out how to have the collision detector do something for each specific piece of ore that passes through
okay, so different kinds of ores might require different buildings to refine
kinda yes
Basically, you need to decide whether each kind of ore:
- gets its own class, like
IronOreandCopperOre - is just an
Orewith some data on it
Here's what the second might be like
public enum OreKind {
Copper,
Iron,
Gold
}
public class Ore : MonoBehaviour {
public string label;
public float value;
public Color color;
public OreKind kind;
}
You'd just have Ore.
this would all go in 1 script?
yeah, one script file
ok
You can only define one MonoBehaviour per script file
But you can have other stuff
ok i see this one
A machine that only refines iron would check if kind equals OreKind.Iron
You could take this a step further, of course...
public enum ItemType {
Ore,
Bar
}
public enum Material {
Copper,
Iron,
Gold
}
public class Item : MonoBehaviour {
public string label;
public float value;
public Color color;
public ItemType type;
public Material material;
}
woah
There is one annoying problem here, though
notice how stuff like label, value, and color are all part of Item
we don't really know how much an iron bar should cost, or what we should call it, or what color it should be
we'd have to manually define it
Have you ever heard of "scriptable objects" before?
i just saw a video on it
in short, they let you make unity objects that aren't in a scene
You could do something like this
[CreateAssetMenu(fileName = "New Material", menuName = "Material")]
public class Material : ScriptableObject {
public string label;
public Color color;
public float valueFactor;
}
[CreateAssetMenu(fileName = "New Item Kind", menuName = "Item Kind")]
public class ItemKind : ScriptableObject {
public string label;
public Mesh mesh;
public float baseValue;
}
public class Item : MonoBehaviour {
public ItemKind itemKind;
public Material material;
}
These would go in three separate script files.
You would create assets for each kind of material and for each kind of item
gah damn
Then you'd just plug them in
This might be too generic for your game, though
it means you can make a gold spring or an iron necklace
so far i think this one is working the best
and ive plugged it in and it worked
Yeah
tysm
From my experience: the more classes you make, the more specific behavior you can create
i see
The fewer classes you make, the more variety you can get
This is super super broad. you can mash together any material and any item kind
And if you want some items to have special behaviors, then you have to figure that out
no problem (:
i'm making a soulslike game with a big pile of scriptable objects defining everything
so i've been going through this too lol
nice