Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Chris42 · Nov 26, 2012 at 06:52 PM · inheritanceconstructors

Constructors always returning void

This issue seems like it's really simple, but am completely failing to get it to work.

I have a basic data class AI order which has one constructor and one overridden method.

 class AIOrder
 {
     var name:String;
     
     // Constructor
     function AIOrder()
     {
         name = "GenericOrder";
     }
     // Overriden
     function ExecuteOrder(brain:ShipAIBehavior)
     {
     }
 }

I extend the class to represent other order types, overriding the Execute function (I have 10 different types here, this is the simples).

 class AttackNearestOrder extends AIOrder 
 {
     function AttackNearestOrder()
     {
     
         name = "AttackNearest";
     }
     
     function ExecuteOrder(brain:ShipAIBehavior)
     {
         brain.OrderWaitForTarget();
     }
 }

It seems like it should be simple enough. I usually access this by something like this...

 var order:AttackNearestOrder = new AttackNearestOrder();
 aiCraft.AddOrderToOrderList(order );

However, the constructor always returns void. If I try `new AIOrder()` an object of type AIOrder is correctly returned by the constructor, just fails when I try to get the extended class.

The funny thing is that this worked when AIOrder and its extended versions where a subclass of another class (that inherited from MonoBehavior). I moved them out as I needed to be able to construct AIOrders from other classes, and now the trouble has started.

Ideas? Feel like it must be very simple.

Comment
Add comment · Show 2
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 Yokimato · Nov 26, 2012 at 06:57 PM 0
Share

Well I'll guarantee that you're constructor is not returning void. It simply can't. So, my guess is that there is a null reference happening in `AddOrderToOrderList.` Can you post the code for that function please (or whatever calling function that throws the error)?

avatar image Chris42 · Nov 26, 2012 at 07:06 PM 0
Share

The error actually occurs in the line before it, it's a "Cannot convert void to AttackNearestOrder" error. So I don't think it has anything to do with AddOrderToOrderList at the moment.

2 Replies

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

Answer by Chris42 · Nov 26, 2012 at 08:05 PM

The problem: The constructor `new AttackNearestOrder()` was conflicting with the name of a method in the script! That method of course had a return type of void and was being called first.

Silly of me!

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 Loius · Nov 26, 2012 at 07:32 PM

I'm not certain it's -necessary-, but I usually use a super call to invoke the parent contructor explicitly:

function Constructor(params) { super(params); }

Knowing how odd some things can be at times, I'd at least give that a try to see if it helps.

Comment
Add comment · Show 1 · 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 Chris42 · Nov 26, 2012 at 08:04 PM 0
Share

This wasn't exactly the answer, but doing this let me to the correct solution (see my answer below).

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The type 'myclass' does not have a visible constructor that matches the argument list 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Having problem with constructor 3 Answers

How to use Inheritance and Constructors in Unity 1 Answer

How to change variables from another script? 1 Answer


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