Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jake_Martin_ · May 24, 2016 at 06:14 PM · c#programmingclassescasting

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jake_Martin_ · May 24, 2016 at 06:42 PM 0
Share
  • 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

avatar image tanoshimi Jake_Martin_ · May 24, 2016 at 09:24 PM 1
Share

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;

?

avatar image Jake_Martin_ tanoshimi · May 24, 2016 at 11:16 PM 0
Share

Thanks, you got it :)

avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Cram variables inside class, use inheritance, or something else? 1 Answer

Casting objects in Unity using a local class of an object 2 Answers

Distribute terrain in zones 3 Answers

How do I reference a class to pass on variables in that class? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges