- Home /
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: [System.Serializable]
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.
Answer by tanoshimi · May 24, 2016 at 06:18 PM
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 memberObstacles
, which is in itself an array ofObstacles[]
...
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.
currentObstacles is of type Obstacles[], which is why it says I can't convert to Obstacles[]
ObstacleTypes is an array of type ObstacleClass. It contains arrays of type Obstacles[]
Yeah, the na$$anonymous$$g is awkward, sorry. Obstcales[] was created in a very early script in the project, the rest of the mess is due to the need to serialize an array of arrays.
Any idea how I can fix it then? Thanks
Well you need to work out what you're trying to do! If currentObstacles is Obstacles [], why are you trying to set it to a single ObstacleClass? Did you mean:
currentObstacles = ObstacleTypes[(int)currentPlanet.obstacleType].Obstacles;
?
Answer by gorsefan · May 25, 2016 at 01:44 AM
The error I'm given is "Cannot implicitly convert type 'WorldDataScript.ObstacleClass' to 'Obstacles[]'"
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 :) The word "Class" in a class name isn't great either IMO...
Answer by ninja_gear · May 25, 2016 at 02:51 PM
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?