• 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
1
Question by gardian06 · Jul 12, 2013 at 10:34 PM · compiler-error

why would List, and Dictionary not resolve

I was looking through this UnityGems post, and grabbed the code from the first comment by farfpeuf (after changing the double quotes to actual double quotes as for some reason they were being read as something else) but I am getting a strange error where even though my using statements are:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;

I still get an error on :

 public Dictionary properties; // inside ScannedScript Class

and

 static List scripts;   // immediatly following the ScannedScript Class definition

even if I give the exact directive of what they are like

  static System.Collections.Generic.List scripts;

where this just makes no sense as why both of these are throwing CS0234 errors because I have all of the needed using directives. does anyone have any ideas of what is going on here?

EDIT: including full script to save the possibility of the followup question:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
 using System;
 
 [ExecuteInEditMode]
 [CustomEditor(typeof(MonoBehaviour))]
 public class MonoScriptEditor : Editor{
     public static bool tried;
     public static GameObject tryThisObject;
 
     class ScriptComparer: IComparer{
         public int Compare(ScannedScript sa, ScannedScript sb){
             return StringComparer.OrdinalIgnoreCase.Compare(sa.script.name,sb.script.name);
         }
     }
     
     public class ScannedScript{
         public Dictionary<string, FieldInfo> properties;
         public int id;
         public MonoScript script;
     }
     static List<ScannedScript> scripts;
     static bool _initialized = false;
     bool _localInit;
     void Initialize(){
         if(!_localInit){
             EditorApplication.projectWindowChanged += ()=>{ Repaint(); };
             _localInit = true;
         }
         if(_initialized){ return; }
         _initialized = true;
         
         ScanAll();
     }
     
     public override void OnInspectorGUI (){
         if(target.GetType()!=typeof(MonoBehaviour) && target.GetType() != typeof(UnityEngine.Object)){
             base.OnInspectorGUI();
             return;
         }
         EditorPrefs.SetBool("Fix", GUILayout.Toggle(EditorPrefs.GetBool("Fix", true), "Fix broken scripts"));
         if(!EditorPrefs.GetBool("Fix", true)){
             GUILayout.Label("*** SCRIPT MISSING ***");
             return;
         }
         Initialize();
         var iterator = this.serializedObject.GetIterator();
         var first = true;
         while(iterator.NextVisible(first)){
             first = false;
             if(iterator.name == "m_Script" && iterator.objectReferenceValue == null){
                 if(tryThisObject == (target as Component).gameObject){
                     tried = true;
                 }
                 var script = iterator.Copy();
                 var candidates = scripts.ToList();
                 while(iterator.NextVisible(false) && candidates.Count>0){
                     candidates = candidates.Where(c=>c.properties.ContainsKey(iterator.name)).ToList();
                 }
                 if(candidates.Count==1){
                     script.objectReferenceValue = candidates[0].script;
                     
                     serializedObject.ApplyModifiedProperties();
                     serializedObject.UpdateIfDirtyOrScript();
                 }else if(candidates.Count > 0){
                     ScriptComparer sc = new ScriptComparer();
                     candidates.Sort(sc);                        // error occures here
                     foreach(var candidate in candidates){
                         if(GUILayout.Button(candidate.script.name)){
                             script.objectReferenceValue = candidate.script;
                             
                             serializedObject.ApplyModifiedProperties();
                             serializedObject.UpdateIfDirtyOrScript();
                         }
                     }
                 }else{
                     GUILayout.Label("> No suitable scripts were found");
                 }
                 break;
             }
         }
         base.OnInspectorGUI ();
     }
     
     void ScanAll(){
         scripts = Resources.FindObjectsOfTypeAll(typeof(MonoScript))
             .Cast<MonoScript>()
                 .Where(c=>c.hideFlags == 0)
                 .Where(c=>c.GetClass() != null)
             .Select(c=>new ScannedScript { id = c.GetInstanceID(),
                     script = c,
                     properties = c.GetClass().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                         .Where(p=>p.IsPublic || (!p.IsPublic && p.IsDefined(typeof(SerializeField), false)))
                             .ToDictionary(p=>p.Name)
                 })
                 .ToList();
     }
 
 }
 

errors are indicated on lines 21, and 25 respectively

Comment
Add comment · Show 1
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 gardian06 · Jul 15, 2013 at 02:24 PM 0
Share

as per @Bunny83 suggested the initial errors have been fixed, but the script does not compile still because I am getting a compiler error on line 71 about cannot convert $$anonymous$$onoScriptEditor.ScriptComparer to System.Collections.Generic.IComparer[$$anonymous$$onoScriptEditor.ScannedScript]

most of the time I see these I can either do a work around conversion, or explicitly cast, but I don't know how to give the compiler what it is asking for as I don't know what is missing

1 Reply

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

Answer by Bunny83 · Jul 12, 2013 at 10:53 PM

Uhm, you're missing the generic parameters on your List and Dictionary type.

The type name should be:

 List<ScannedScript>

and

 Dictionary<string, FieldInfo>

Comment
Add comment · Show 4 · 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 Bunny83 · Jul 12, 2013 at 10:56 PM 0
Share

ps: the comments on Unity Gems got their HT$$anonymous$$L tags stripped aways since they don't have a code section. That's why the generic parameters are missing in the comments. Reading the actual post might help ;)

avatar image gardian06 · Jul 15, 2013 at 02:26 PM 0
Share

I feel really smart right now.

avatar image Bunny83 · Jul 15, 2013 at 02:37 PM 0
Share

I'm pretty sure it's the same problem again ;) He probably used the generic IComparer interface, so your ScriptComparer class should implement this interface:

     : IComparer< ScannedScript >
avatar image gardian06 · Jul 15, 2013 at 02:44 PM 0
Share

fully functional now. sorry for not realizing the template arguments were missing, and thank you for not deriding.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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 avatar image avatar image avatar image

Related Questions

Unity 3d 4.6.f3 Could not find Csharp Compiler. Please help me? 1 Answer

"All compiler errors have to be fixed before you can enter playmode!" 1 Answer

How do you use a script in another script? 1 Answer

Compile Error ("boolean var" is not a member of 'UnityEngine.Component') User script 0 Answers

Command/ClientRpc with generic parameter 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