#I m having a tiny bit of issues trying
1 messages · Page 1 of 1 (latest)
Fair point
classes that inherit from a class that extends monobehavior should be able to be attatched as a component, I do it all the time.
perhaps I did something wrong
can you just show the signatures of the classes?
public abstract class Block : MonoBehaviour
{
protected float Health = 100;
protected float Mass = 1; //update these to realistic values
protected int cost;
// Add any common properties or methods for all blocks here.
}
public class MovementBlock : Block
{
public Fuel FuelType { get; }
public float HeatGeneration { get; }
public float ThrustPower { get; }
public float RotationPower { get; }
public MovementBlock(Fuel fuelType, float heatGeneration, float thrustPower, float rotationPower)
{
FuelType = fuelType;
HeatGeneration = heatGeneration;
ThrustPower = thrustPower;
RotationPower = rotationPower;
}
}```
Also not sure what signature of a class means, can you explain briefly?
im not even sure if its a real term, I was just using it bc it's similar to the signature of a meathod. I just meant the public abstract class Block : MonoBehaviour parts.
so, you can't attatch MovementBlock to a gameObject?
(also, it's not c# it's cs for formatting)
click add component and search Block, you don't see it?
wait, are these both in one file?
yes, though chatGBT told me that shouldn't matter, and I don't want to bother you guys with everything so i was trying to use it when I can
it does matter if ur trying to add it through UI. Remember, chatGPT is a language model, it doesn't search the internet. you shouldn't take what it says as fact.
You can add the component through code, but for it to show up in the inspector it needs to be the first class in a file named the same as the class.
yep, but it can be very helpful if you get your phrasing correct, but yes it's limitations are big, often it makes up imaginary functions
Thank you! is there any way around that, or will my folders just get a little messy?
just have a folder called Blocks and dump all your block scripts in there. If you want them all in one script you'll have to add them to the objects through code.
so for what you showed, you'd need a script called MovementBlock with the first class in the file named MovementBlock.
I'll go with the folder method since I'm still learning and the unity UI is more familiar with me so far, thank you for the info friend!
Ok, everything should be good but the fields wont show up just yet
Any idea?
this is in a prefab is that is important context, and its got the funny icon so I guess its an override? I don't know how to turn that off @supple torrent
those aren't fields those are properties
readonly properties at that
also you shouldn't use a constructor for a monobehavior, I didnt catch that before.
since it will make tons of instances right?
because unity just won't use it and you won't use it
good to know
change those properties to fields.
https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property
depending on what Fuel is yea
one sec
namespace Content
{
[System.Serializable]
public class Fuel
{
public string FuelName { get; }
public Color FuelColor { get; }
public float BasePricePerUnit { get; }
public Fuel(string fuelName, Color fuelColor, float basePricePerUnit)
{
FuelName = fuelName;
FuelColor = fuelColor;
BasePricePerUnit = basePricePerUnit;
}
public float CalculateSellPrice(float economyFactor)
{
// Calculate sell price based on economy factor and base price
return BasePricePerUnit * economyFactor;
}
// Add any other methods or properties related to Fuel here.
}
public class ElectricCharge : Fuel
{
public ElectricCharge() : base("Electric Charge", new Color(1.0f, 0.8f, 0.4f), 0f)
{
// Additional initialization specific to ElectricCharge class if needed.
}
}```
put [System.Serializable] above the class then it will work, or else it won't be serialized into the inspector.
this is the framework/outline I got so far
will do
Shoot still nothing
do you mean each sub class?
like ElectricCharge
yea everythng that's a class or struct or record u want to show up in the inspectr
ok ill try slapping it in a few more places haha
also those are readonly properties not fields
Ok, so my structure is incorrect, I probably did that everywhere
not to sound too rude, but do you know what you're trying to do by putting these?
public string FuelName { get; }
public Color FuelColor { get; }
public float BasePricePerUnit { get; }
No offence taken, it allows me to use a getter to access them from other classes correct?
does not allow for setting though
This is the first time I have been working on this project after a break so I cannot recall exactly why I had chosen to do that
My goal is to have a abstract structure for all my blocks and their subtypes, then I wanted to create prefabs with each subtype and have them also have properties from block which was the abstract
yes it's a getter, but 1) it's not getting anything and 2) properties can't be serialized
Also, i suspect your IDE is not set up because that should be giving you an error.
I think i setup my ide properly?
nevermind, i guess it only does that for set only
gotcha
I'll go back and change them all, should I still have the begginging parent class which is abstract extend monobehavoir?
if you want properties to private fields that's fine. You can put [SerializeField] above/next to private fields that you return through your properties and those private fields will show in the inspector.
also is what I'm trying to create called a structure?
idk
crap, im still learning terminology
im not familiar with all the terminology myself so maybe it is.
But you don't have to go back and change verything, just make those getter properties return private fields with the SerializeField applied to them and it will all work good
also regarding constructors in the class that I showed you, I recall my reasoning was since my load function was massive with all of the contstructors, so I moved them to the classes they were in
is there a better way of approaching that problem?
will do
quick question will it be easier for me later to change the getters to normal fields or just add the getters that return private fields?
what do you mean?
doesn't really matter imo. Most unity devs forgo getters/setters however.
ok, if its common practice I'll go with that
and how can i make sure my abstract properties are also carried over or can I only do that with scripts?
For example health, mass ect
I hate to ping again @supple torrent are you busy? if so I can bother somone else no worries :)
im not sure I understand
Health, Mass, and Cost will all be displayed in the inspector of classes that extend Block
is it possible for my movement block class to have health mass and cost serizable
in the same component?
hmm, it should be displaying
maybe it doesn't display protected
put [SerializeField] next to each one
perfect! that did the trick
lastly how do I get unity to recognize my custom fuels in the class fuel
using System.Collections.Generic;
using UnityEngine;
namespace Content
{
[System.Serializable]
public class Fuel
{
public string FuelName { get; }
public Color FuelColor { get; }
public float BasePricePerUnit { get; }
public Fuel(string fuelName, Color fuelColor, float basePricePerUnit)
{
FuelName = fuelName;
FuelColor = fuelColor;
BasePricePerUnit = basePricePerUnit;
}
public float CalculateSellPrice(float economyFactor)
{
// Calculate sell price based on economy factor and base price
return BasePricePerUnit * economyFactor;
}
// Add any other methods or properties related to Fuel here.
}
[System.Serializable]
public class ElectricCharge : Fuel
{
public ElectricCharge() : base("Electric Charge", new Color(1.0f, 0.8f, 0.4f), 0f)
{
// Additional initialization specific to ElectricCharge class if needed.
}
}
[System.Serializable]
public class HydrogenFuel : Fuel
{
public HydrogenFuel() : base("Hydrogen Fuel", new Color(0.0f, 0.5f, 1.0f), 1f)
{
// Additional initialization specific to HydrogenFuel class if needed.
}
}
[System.Serializable]
public class PetroleumFuel : Fuel
{
public PetroleumFuel() : base("Petroleum Fuel", new Color(1.0f, 0.1f, 0.1f), 2f)
{
// Additional initialization specific to PetroleumFuel class if needed.
}
}
}```
can you explain the problem a bit more
here it won't show my fuel type that I tried to make serializable
public class MovementBlock : Block
{
public Fuel fuelType;
public float heatGeneration = 0f;
public float thrustPower = 0f;
public float rotationPower = 0f;
}
Well there's nothinig to show - Fuel is all properties
are my classes that extend fuel not actually constructing a fuel type?
they are, but Fuel only has properties in it, so when you put it in the inspector it won't show anything - remember, they have to be serialized fields (or public fields). Are you wanting it to show like a field you can drag and drop stuff onto?
mainly I want to just be able to select one of the fuels I have so far
like a drop down
you can use a custom inspector or you can have it an enum
what I suggest is make an enum that stores the name of every fuel, then in Start you can use that enum to set a non-serialized Fuel field with the correct fuel.
if you don't want to do that, you can make a custom inspector