unintended code output pls help

hello all, I’m starting work on a 2d space game I set up a class for ships, and a class for fleets
they compile perfect and I added the fleet script to my player and get an unusual output.In the class Fleet I make 5 ship objects and pass in the health, defense and attack. When I add it to my player it sets all those values to 0

3223451–247176–Fleet.cs (2.11 KB)
3223451–247177–Ship.cs (310 Bytes)
3223451--247178--fleetproblem.jpg

Did you attach the script before initialising those variables? Try clicking the little cog in the top right next to the script name in the inspector and click Reset.
(Note this will reset it to its default state which should be with those values you set within your script)

so when its reset it should all be 0 since the variables for the number of each type of ship are uninitialized, but when I change the number in the inspector I thought the attack, defense, health for that ship type would go up. instead it stays at 0

If you look here: Using code tags properly
it will tell how you how to add code (looking good and easy to read for us).
Rather than downloading files…

Thanks, I was looking for that. Ill repost
my code after work today

Ship class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Ship
{
    public int health;
    public int defense;
    public int attk;

    public Ship(int h, int d, int a)
    {
        health = h;
        defense = d;
        attk = a;
    }

}

My fleet class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Fleet : MonoBehaviour
{
    public int numSloop;
    public int numCorsair;
    public int numFrigate;
    public int numCarvahal;
    public int numWarShip;

    public Fleet(int s, int cor, int frig, int carv, int ws)
    {
        numSloop = carv;
        numCorsair = cor;
        numFrigate = frig;
        numCarvahal = carv;
        numWarShip = ws;
    }
    // set up Ship objects
    public Ship sloop = new Ship(1, 2, 2);
    public Ship corsair = new Ship(5, 2, 2);
    public Ship frigate = new Ship(10, 4, 4);
    public Ship carvahal = new Ship(4, 10, 4);
    public Ship warship = new Ship(15, 10, 15);

    public int Attack()
    {
        int totalAttack;
        totalAttack = (sloop.attk * numSloop) + // collect attack power * numer of current ships
                      (corsair.attk * numCorsair) + // for each ship type
                      (frigate.attk * numFrigate) +
                      (carvahal.attk * numCarvahal) +
                      (warship.attk * numWarShip);
        return totalAttack;// returns total attack power for fleet

    }

    public int Defend()
    {
        int totalDefense;
        totalDefense = (sloop.defense * numSloop) + // collect Defense * numer of current ships
                      (corsair.defense * numCorsair) + // for each ship type
                      (frigate.defense * numFrigate) +
                      (carvahal.defense * numCarvahal) +
                      (warship.defense * numWarShip);
        return totalDefense;// returns total Defense for fleet

    }

    public int Health()
    {
        int totalAttack;
        totalAttack = (sloop.health * numSloop) + // collect health * number of current ships
                      (corsair.health * numCorsair) + // for each ship type
                      (frigate.health * numFrigate) +
                      (carvahal.health * numCarvahal) +
                      (warship.health * numWarShip);
        return totalAttack;// returns total Health for fleet

    }
 
}

In my fleet class i make 5 ship objects and pass numbers to the ship constuctor, but it seems like they all stay at 0. I’m sure I’m just missing something fundamental

1 thing of note – the monobehaviour ‘fleet’ : it’s not advisable to use constructors. Use Awake or Start
to do your setup.

Okay, I tested it…

  // Set these variables in the inspector (you could use a method here, too, just not a constructor)
    public int numSloop;
    public int numCorsair;
    public int numFrigate;
    public int numCarvahal;
    public int numWarShip;

    // I tested this, and I got zero, too, if I created them here.
    public Ship sloop;
    public Ship corsair;
    public Ship frigate;
    public Ship carvahal;
    public Ship warship;
    private void Awake()
    {
        // set up Ship objects
        sloop = new Ship(1, 2, 2);
        corsair = new Ship(5, 2, 2);
        frigate = new Ship(10, 4, 4);
        carvahal = new Ship(4, 10, 4);
        warship = new Ship(15, 10, 15);
    }

That should work for ya.

However, I should point out that it only updates when your run the game this way.
So, it depends on how you want to do this. You could add the script to your game object, and then set the values there, instead. If you did that, you could remove these lines in Awake and that would work, also. Then you could edit it in the inspector.

1 Like

Love it, ill try this right away. My idea is I want to set up another class to increment the number of each ship in the fleet based off time passing, but I ran into this problem right off

Cool hope you get it working how you’d like :slight_smile:

1 Like

So my code was basically correct, just since it wasn’t In awake or update there couldn’t be anything there

In this case, Awake or Start… yes, most of it was working (your code).

As I mentioned, you could also have assigned the script to the player and filled the variables in there (in the inspector). That’s handy if you are still working on the variable values or think you might want to change them… because then you can alter them in the inspector, and you don’t have to re-visit the code (like in my example - in Awake() to alter them).

Just got home and the code works beautifully thanks so much, I hope to soon be able to post a finished game

Cool glad it’s working :slight_smile:

1 Like