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 Zak_33 · Mar 16, 2016 at 12:38 PM · bugcompiler

Possibly fake errors in Unity

Hello,

I have a script for which I am getting the three errors below in Unity. However, they don't show up in Visual Studio which usually spots them.

I have tried to reimport all assets of the project, but I am still getting the errors.

 Assets/Comparers/PropertyComparer.cs(28,27): error CS1729: The type `Comparers.PropertyComparer<T>' does not contain a constructor that takes `2' arguments
 
 Assets/Comparers/PropertyComparer.cs(64,25): error CS0103: The name `ValidateProperty' does not exist in the current context
 
 Assets/Comparers/PropertyComparer.cs(80,25): error CS0103: The name `ValidateProperty' does not exist in the current context

And here is the complete code:

 using System;
 using System.Collections.Generic;
 using System.Reflection;
 
 namespace Comparers
 {
     /// <summary>
     /// Defines the directions in which collection items may be sorted.
     /// </summary>
     public enum SortDirection : byte
     {
         Ascending,
         Descending
     }
 
     /// <summary>
     /// Supports the comparison of two objects based on a specific property.
     /// </summary>
     public class PropertyComparer<T> : Comparer<T>
     {
         private readonly PropertyInfo property;
         private readonly SortDirection direction;
 
         /// <summary>
         /// Constructor.
         /// </summary>
         public PropertyComparer()
             : this(string.Empty, SortDirection.Ascending)
         {
         }
 
         /// <summary>
         /// Constructor.
         /// </summary>
         public PropertyComparer(string propertyName, SortDirection direction = SortDirection.Ascending)
         {
             Type type = typeof(T);
 
             if (string.IsNullOrEmpty(propertyName))
             {
                 PropertyInfo[] properties = type.GetProperties();
 
                 if (properties.Length > 0)
                 {
                     property = properties[0];
                 }
                 else
                 {
                     throw new MissingMemberException(string.Format("{0} does not contain any properties", type.Name));
                 }
             }
             else
             {
                 property = type.GetProperty(propertyName);
 
                 if (property == null)
                 {
                     throw new MissingMemberException(string.Format("{0} does not contain a property named \"{1}\"", type.Name, propertyName));
                 }
             }
 
             this.direction = direction;
 
             ValidateProperty(type, false);
         }
 
         /// <summary>
         /// Constructor.
         /// </summary>
         public PropertyComparer(PropertyInfo propertyInfo, SortDirection direction = SortDirection.Ascending)
         {
             if (propertyInfo == null)
             {
                 throw new ArgumentNullException("propertyInfo");
             }
 
             this.property = propertyInfo;
             this.direction = direction;
 
             ValidateProperty(typeof(T), true);
         }
 
         /// <summary>
         /// Compares two objects based on a specific property.
         /// </summary>
         public override int Compare(T x, T y)
         {
             IComparable a = property.GetValue(x, null) as IComparable;
             IComparable b = property.GetValue(y, null) as IComparable;
 
             int result = a.CompareTo(b);
 
             if (direction == SortDirection.Descending)
             {
                 result *= -1;
             }
 
             return result;
         }
 
         /// <summary>
         /// Validates whether the property used for this comparison is valid.
         /// </summary>
         private void ValidateProperty(Type type, bool checkOwner)
         {
             if (checkOwner && property.DeclaringType != type)
             {
                 throw new MissingMemberException(string.Format("Property {0} is not a member of {1}", property.Name, type.Name));
             }
 
             if (!property.CanRead)
             {
                 throw new MemberAccessException(string.Format("Property {0} of {1} can't be read", property.Name, type.Name));
             }
         }
     }
 }

Any pointers on how to solve this are appreciated!

Credits for the original code: https://bitbucket.org/KornnerStudios/ksoft/src/afc1d080fc5540f9d2d4d08f80cfe5c9d152f31c/KSoft/Collections/PropertyComparer.cs?at=default&fileviewer=file-view-default

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

1 Reply

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

Answer by Zak_33 · Mar 14, 2016 at 11:03 PM

I found the reason for the errors: the Unity compiler does not support optional parameters on constructors. Upon removing them, the code compiles properly. I guess it's a Mono-related thing so there's nothing we could do...

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Bug hunting in the code... 1 Answer

my mistake or bug? Unity compiler error 0 Answers

Microsoft vs Mono C# compiler initialisation differences. Is this a bug in Mono? 2 Answers

Bug with Unity Compiler with Lambdas? 0 Answers

Changing Skybox tint in game, permanently changes it in editor? 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