Error

Assets\Creator Kit - Beginner Code\Scripts\Tutorial\SpawnerSample.cs(11,9): error CS1520: Method must have a return type

using CreatorKitCode;
public class SpawnerSample : MonoBehaviour
{
    public class lootAngle
    {
        int angle;
        int step;
        LootAngle(int increment)
        {
            step = increment;
            angle = 0;
        }
        int nextAngle()
        {
            int currentAngle = angle;
            angle=Helpers.WrapAngle(angle + step);
            return currentAngle;
        }
    }
    public GameObject ObjectToSpawn;
    void Start()
    {
        LootAngle myLootAngle = new LootAngle(45);
        SpawnPotion(myLootAngle.NextAngle());
        SpawnPotion(myLootAngle.NextAngle());
        SpawnPotion(myLootAngle.NextAngle());
        SpawnPotion(myLootAngle.NextAngle());
        void spawnPotions(int angle)
        {
            int radius = 5;
            Vector3 spawnPosition = transform.position;
            Vector3 direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
            spawnPosition = transform.position + direction * radius;
            Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
        }
    }
}

Please use Code Tags in the future, otherwise reading code is very hard.

Change:

public class lootAngle

to (note the capital L):

public class LootAngle

1 Like

When you write a method, it must always have a return type. If the method does not return anything, then you have to use “void”- so instead of

LootAngle(int increment)
{
  step = increment;
  angle = 0;
}

you would need to write:

void LootAngle(int increment)
{
  step = increment;
  angle = 0;
}
1 Like

Unless it is a constructor, which I am guessing it is supposed to be here.

2 Likes

Oops. I didn’t notice there was another class inside SpawnerSample. Edit: I see the problem. the class is called lootAngle, but the constructor is called LootAngle. The names don’t match, so the compiler does not know it’s supposed to be a constructor (neither did I, actually). :sweat_smile:

Is it supposed to be a constructor?

2 Likes

I see a number issues.

  • lootAngle needs to be LootAngle like @bobisgod234 said

  • you call SpawnPotion several times but the method name is spawnPotions

  • At a glance you won’t even be able to create a LootAngle because the constructor is private. Make it public.

  • Method name is nextAngle but you are calling NextAngle. nextAngle also needs to be public or you won’t be able to access it.

  • You can create the method spawnPositions inside of a method like you are doing here, but… don’t for this case. You’ll no doubt be using spawnPositions again somewhere else in the code, don’t restrict it to this one method.

I think this will be a good exercise for you to try to work out yourself. See how far you can get with that info.

2 Likes

I saw your first comment, but then saw you corrected yourself. You had me looking back at my post, thought I lost my mind for a second, lol.

1 Like

Thanks All. I’m still relatively new to Unity so I need to wrap my head around a few things.

Geez How did I not notice that the method name was right under where spawnPotions was.

No worries. Keep coding and correcting yourself, you’ll get there eventually.