Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

the type or namespace name could not be found [C#]

Discussion in 'Scripting' started by Tartan Spartan, Aug 24, 2015.

  1. Tartan Spartan

    Tartan Spartan

    Joined:
    Aug 24, 2015
    Posts:
    2
    I have an issue using the Rust sdk in Unity (Rust as in https://www.reddit.com/r/playrust/)

    The script the Dev's provided to upload your creations into the Steam workshop seem to be broken.

    upload_2015-8-24_11-32-1.png

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. [CreateAssetMenu( menuName = "Scriptable Object/Workshop Skin Meta", fileName = "meta.asset" )]
    5. public class WorkshopSkin : WorkshopBase
    6. {
    7.     public enum SkinType : int
    8.     {
    9.         TShirt,
    10.         Pants,
    11.         SleepingBag,
    12.         Hoodie,
    13.         LongTShirt,
    14.         Cap,
    15.         Beenie,
    16.         Boots,
    17.         Jacket,
    18.         Balaclava,
    19.         Boonie,
    20.         SnowJacket
    21.     }
    22.  
    23.     public static string[] itemName =
    24.     {
    25.         "tshirt",
    26.         "pants",
    27.         "sleepingbag",
    28.         "hoodie",
    29.         "tshirt.long",
    30.         "hat.cap",
    31.         "hat.beenie",
    32.         "shoes.boots",
    33.         "jacket",
    34.         "mask.balaclava",
    35.         "hat.boonie",
    36.         "jacket.snow"
    37.     };
    38.  
    39.     [Header( "Skin Setup" )]
    40.     public SkinType skinType;
    41.     public Material skinMaterial;
    42.  
    43. #if UNITY_EDITOR
    44.  
    45.     public override void StartPreview()
    46.     {
    47.         var oldPreview = GameObject.Find( "PreviewPrefab" );
    48.         if ( oldPreview != null )
    49.         {
    50.             GameObject.DestroyImmediate( oldPreview, true );
    51.         }
    52.  
    53.         var prefabName = "Assets/Content/Skinnable/" + skinType.ToString() + "/preview.prefab";[ATTACH]151547[/ATTACH] [ATTACH]151547[/ATTACH]
    54.         var prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>( prefabName );
    55.         if ( prefab  == null )
    56.         {
    57.             Debug.LogError( "Couldn't Find: " + prefabName );
    58.             return;
    59.         }
    60.  
    61.         var pf = GameObject.Instantiate<GameObject>( prefab );
    62.  
    63.         foreach ( var obj in pf.GetComponentsInChildren<Renderer>())
    64.         {
    65.             if ( !obj.CompareTag( "skin0" ) ) continue;
    66.  
    67.             var mats = obj.sharedMaterials;
    68.             mats[0] = skinMaterial;
    69.             obj.sharedMaterials = mats;
    70.         }
    71.  
    72.         pf.name = "PreviewPrefab";
    73.         pf.transform.position = Vector3.zero;
    74.         pf.transform.rotation = Quaternion.identity;
    75.     }
    76.  
    77.     public override List<string> GetTags()
    78.     {
    79.         return new List<string>() { "Skin", skinType.ToString() + " Skin" };
    80.     }
    81.  
    82. #endif
    83. }
    84.  
     
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Doesn't look like you are actually referencing the corresponding namespace.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections.Generic
    Not sure which one to use, however right now your project does not know about "CreateAssetMenu".

    Edit: Seems like it should be part of UnityEngine.... too early, sorry.

    Edit2: Just tried it in a blank project and it works out of the box. There are some more errors in the code according to your screenshot, so it might be good to fix those first to make sure it's not some kind of compilation chain effect.
     
    Last edited: Aug 24, 2015
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    Are you using Unity 4? The CreateAssetMenu attribute did not exist in Unity 4 - at least it's not in the docs.
     
  4. Tartan Spartan

    Tartan Spartan

    Joined:
    Aug 24, 2015
    Posts:
    2
    Thanks, I did try and add
    Code (CSharp):
    1. using UnityEditor;
    but then realised the same as you, it's in Engine.
    The other error exists in this .cs

    FYI - none of this work is my own, it's owned by Facepunch and is part of their SDK.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.Linq;
    5.  
    6. public class DuplicateWithReferences : MonoBehaviour
    7. {
    8.     [MenuItem( "Assets/Tools/Duplicate With References %#d" )]
    9.     private static void DoRun()
    10.     {
    11.         var path = "";
    12.  
    13.         // Try to work out what folder we're clicking on. This code is from google.
    14.         foreach ( UnityEngine.Object obj in Selection.GetFiltered( typeof( UnityEngine.Object ), SelectionMode.Assets ) )
    15.         {
    16.             path = AssetDatabase.GetAssetPath( obj );
    17.             if ( System.IO.File.Exists( path ) )
    18.             {
    19.                 path = System.IO.Path.GetDirectoryName( path );
    20.                 break;
    21.             }
    22.         }
    23.  
    24.         // Not a directory - bail
    25.         if ( !System.IO.Directory.Exists( path ) )
    26.             return;
    27.  
    28.         // Create a "<name> 1" path
    29.         var targetPath = AssetDatabase.GenerateUniqueAssetPath( path );
    30.  
    31.         // Do the business
    32.         Duplicate( path, targetPath );
    33.  
    34.         EditorUtility.ClearProgressBar();
    35.     }
    36.  
    37.     public static void Duplicate( string from, string to )
    38.     {
    39.         EditorUtility.DisplayProgressBar( "Duplicate With References", "Copying " + from, 0.1f );
    40.         AssetDatabase.CopyAsset( from, to );
    41.         EditorUtility.ClearProgressBar();
    42.  
    43.         EditorUtility.DisplayProgressBar( "Duplicate With References", "Importing " + to, 0.3f );
    44.         AssetDatabase.ImportAsset( to, ImportAssetOptions.ImportRecursive );
    45.         EditorUtility.ClearProgressBar();
    46.  
    47.         EditorUtility.DisplayProgressBar( "Duplicate With References", "Fixing References " + to, 0.7f );
    48.         FixReferences( from, to );
    49.         EditorUtility.ClearProgressBar();
    50.  
    51.         EditorUtility.DisplayProgressBar( "Duplicate With References", "Saving " + to, 1.0f );
    52.         AssetDatabase.SaveAssets();
    53.         EditorUtility.ClearProgressBar();
    54.  
    55.         EditorUtility.ClearProgressBar();
    56.     }
    57.  
    58.     public static void FixReferences( string from, string to )
    59.     {
    60.         foreach ( var obj in AssetDatabase.FindAssets( "", new[] { to } ).Select( x => AssetDatabase.LoadAssetAtPath<Object>( AssetDatabase.GUIDToAssetPath( x ) ) ) )
    61.         {
    62.             JoinReferences( from, to, obj );
    63.  
    64.             if ( obj is GameObject )
    65.             {
    66.                 foreach ( var c in ( obj as GameObject ).GetComponentsInChildren<Component>( true ) )
    67.                 {
    68.                     JoinReferences( from, to, c );
    69.                 }
    70.             }
    71.         }
    72.     }
    73.  
    74.     static void FilesRecursive( string folder, System.Action<string> func )
    75.     {
    76.         foreach ( var file in System.IO.Directory.GetFiles( folder ) )
    77.         {
    78.             if ( System.IO.Directory.Exists( file ) )
    79.             {
    80.                 FilesRecursive( file, func );
    81.                 continue;
    82.             }
    83.  
    84.             func( file );
    85.         }
    86.     }
    87.  
    88.     //
    89.     // Replace any references to files in Source with Target (same filenames etc)
    90.     //
    91.     static void JoinReferences( string strSource, string strTarget, Object obj )
    92.     {
    93.         SerializedObject so = new SerializedObject( obj );
    94.         var sp = so.GetIterator();
    95.  
    96.         while ( sp.Next( true ) )
    97.         {
    98.             if ( sp.propertyType != SerializedPropertyType.ObjectReference ) continue;
    99.             if ( sp.objectReferenceValue == null ) continue;
    100.  
    101.             var oldPath = AssetDatabase.GetAssetPath( sp.objectReferenceValue );
    102.  
    103.             if ( !oldPath.StartsWith( strSource, System.StringComparison.InvariantCultureIgnoreCase ) ) continue;
    104.  
    105.             var newPath = oldPath.Replace( strSource, strTarget );
    106.             var newObj = AssetDatabase.LoadAssetAtPath( newPath, sp.objectReferenceValue.GetType() );
    107.             if ( newObj == null )
    108.                 continue;
    109.  
    110.             sp.objectReferenceValue = newObj;
    111.         }
    112.  
    113.         so.ApplyModifiedProperties();
    114.     }
    115.  
    116. }
    117.  

    I'm using unity 5.0.0f4
     
    Last edited: Aug 24, 2015
  5. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    213
    It was added in 5.1