Retrieving data from a custom class

I have a class called ObstacleClass which contains an array of a custom Obstacles class.
I need to retrieve that array as an Obstacle, but I’m having a few issues.

Here’s my code to initialise that class:

    public class ObstacleClass
        public ObstacleClass(Obstacles[] init)
            Obstacles = init;
        public Obstacles[] Obstacles;

And here’s what I’m trying to do with it later:

currentObstacles = ObstacleTypes[(int)currentPlanet.obstacleType];

The error I’m given is “Cannot implicitly convert type ‘WorldDataScript.ObstacleClass’ to ‘Obstacles’”

I’d appreciate some swift help on this, as it’s holding up the rest of my project. Thanks in advance.

You have a very confusing naming convention, and only given us partial information:

  • A variable currentObstacles. What type is it?
  • An array ObstacleTypes[]. What is it an array of?
  • A class ObstacleClass, which has a member Obstacles, which is in itself an array of Obstacles[]

It’s the final point that is causing you the error - you can’t define a variable to be an array of itself, but I think you could do with renaming your variables and class types throughout to be more consistent.

The error I’m given is “Cannot
implicitly convert type
‘WorldDataScript.ObstacleClass’ to

This implies you are expecting an array of Obstacles somewhere and should be using ObstacleClass.Obstacles not just ObstacleClass

But yeah that naming convention is making me go cross-eyed :slight_smile: The word “Class” in a class name isn’t great either IMO…

currentObstacles = ObstacleTypes[(int)currentPlanet.obstacleType];

not sure what you are trying to do if:

‘currentObstacles is of type Obstacles

but then:

     currentObstacles = obstacleClassInstance.Obstacles; 

should fix the error?