#I m having a tiny bit of issues trying

1 messages · Page 1 of 1 (latest)

supple torrent
#

we can start a thread

gusty steeple
#

Fair point

supple torrent
#

classes that inherit from a class that extends monobehavior should be able to be attatched as a component, I do it all the time.

gusty steeple
#

perhaps I did something wrong

supple torrent
#

can you just show the signatures of the classes?

gusty steeple
#
    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?

supple torrent
#

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)

gusty steeple
#

no, for some reason, which is odd

#

aha thank you^

supple torrent
#

click add component and search Block, you don't see it?

#

wait, are these both in one file?

gusty steeple
gusty steeple
supple torrent
#

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.

gusty steeple
gusty steeple
supple torrent
#

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.

gusty steeple
#

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

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.

gusty steeple
#

since it will make tons of instances right?

supple torrent
#

because unity just won't use it and you won't use it

gusty steeple
#

good to know

supple torrent
gusty steeple
#

so with this it still doesnt work

#

public Fuel FuelType;

supple torrent
#

depending on what Fuel is yea

gusty steeple
#

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.
        }
    }```
supple torrent
#

put [System.Serializable] above the class then it will work, or else it won't be serialized into the inspector.

gusty steeple
#

this is the framework/outline I got so far

#

will do

#

Shoot still nothing

#

do you mean each sub class?

#

like ElectricCharge

supple torrent
#

yea everythng that's a class or struct or record u want to show up in the inspectr

gusty steeple
#

ok ill try slapping it in a few more places haha

supple torrent
#

also those are readonly properties not fields

gusty steeple
#

Ok, so my structure is incorrect, I probably did that everywhere

supple torrent
#

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; }
gusty steeple
#

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

supple torrent
#

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.

gusty steeple
#

I think i setup my ide properly?

supple torrent
#

nevermind, i guess it only does that for set only

gusty steeple
#

gotcha

#

I'll go back and change them all, should I still have the begginging parent class which is abstract extend monobehavoir?

supple torrent
#

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.

gusty steeple
#

also is what I'm trying to create called a structure?

supple torrent
#

idk

gusty steeple
#

crap, im still learning terminology

supple torrent
#

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

gusty steeple
#

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?

gusty steeple
supple torrent
#

what do you mean?

gusty steeple
#

lemme edit

#

fixed

supple torrent
#

doesn't really matter imo. Most unity devs forgo getters/setters however.

gusty steeple
#

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 :)

supple torrent
#

Health, Mass, and Cost will all be displayed in the inspector of classes that extend Block

gusty steeple
#

is it possible for my movement block class to have health mass and cost serizable

supple torrent
#

hmm, it should be displaying

#

maybe it doesn't display protected

#

put [SerializeField] next to each one

gusty steeple
#

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.
        }
    }
}```
supple torrent
#

can you explain the problem a bit more

gusty steeple
#
    public class MovementBlock : Block
    {
        public Fuel fuelType;
        public float heatGeneration = 0f;
        public float thrustPower = 0f;
        public float rotationPower = 0f;
  }
supple torrent
#

Well there's nothinig to show - Fuel is all properties

gusty steeple
#

are my classes that extend fuel not actually constructing a fuel type?

supple torrent
#

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?

gusty steeple
#

mainly I want to just be able to select one of the fuels I have so far

#

like a drop down

supple torrent
#

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

gusty steeple
#

Enum sounds like my best bet, awesome thanks for the info

#

Appreciate the time you've taken to help me bro