Search Unity

The name 'PrefabUtility' does not exist in the current context

Discussion in 'Scripting' started by Spraxs, Sep 16, 2018.

Thread Status:
Not open for further replies.
  1. Spraxs

    Spraxs

    Joined:
    Sep 16, 2018
    Posts:
    2
    Hello I am trying to make a copy of a prefab and then disconnect the copy from the prefab. This code works fine when I run my game in Unity, but when I am trying to build my game it gives me an error.



    This is my code:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. public class UserModule : MonoBehaviour {
    6.  
    7.     private IDictionary<string, User> users = new Dictionary<string, User>();
    8.  
    9.     public GameObject userObject;
    10.  
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     public User getUser(string id)
    17.     {
    18.         return users[id];
    19.     }
    20.  
    21.     public User createNewUser(string id, float x, float y)
    22.     {
    23.         Debug.Log("Creating new User..");
    24.         GameObject gameObject = Instantiate(userObject, new Vector3(x, y, 0f), Quaternion.identity);
    25.  
    26.         PrefabUtility.DisconnectPrefabInstance(gameObject);
    27.  
    28.         User user = gameObject.GetComponent<User>();
    29.  
    30.         user.setup(id);
    31.  
    32.         users.Add(id, user);
    33.  
    34.         return user;
    35.     }
    36.  
    37.  
    38.  
    39.     public void removeUser(User user)
    40.     {
    41.         user.remove();
    42.         users.Remove(user.getID());
    43.     }
    44. }
    45.  
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    That's an Editor only function (Using UnityEditor), the script should reside in an Editor folder, and won't be available at run time.
     
    Bunny83 and Mr_DumDUm like this.
  3. vaibhav777

    vaibhav777

    Joined:
    Dec 23, 2020
    Posts:
    1
    Prefab.Utility doesn't exist plz help
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    PrefabUtility exists all day long... ONLY in UnityEditor namespace.

    If you want to use this in your game build, YOU CANNOT.

    If you want you can use conditional compilation directives to remove the code during build time.

    https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    The directive you want is likely UNITY_EDITOR
     
    Bunny83 likes this.
  5. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Sorry to necro, but I get this error in a script I have despite using the UnityEditor namespace, it doesn't prevent me from playing, but it does prevent me from building lol. Any idea how to fix this? I have an object spawner that i used for spawning objects in the scene camera with a right click,
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class ObjectSpawner : MonoBehaviour
    7. {
    8.     public bool editMode, placingWater;
    9.     [SerializeField] bool addRotation;
    10.     public Transform parent,waterParent;
    11.  
    12.     public GameObject[] objsToSpawn;
    13.     [SerializeField] GameObject water;
    14.     public Vector2 sizeRange;
    15.  
    16.     public GameObject[] propObjects;
    17.  
    18.     List<GameObject> spawnedObjects;
    19.  
    20.     [SerializeField] int numObjectsToSpawn;
    21.  
    22.     [SerializeField] float waterYPos;
    23.  
    24.     [SerializeField]Vector2 waterOffset;
    25.  
    26.     List<GameObject> waters;
    27.  
    28.     public void SpawnRandomObjects()
    29.     {
    30.         spawnedObjects = new List<GameObject>();
    31.     }
    32.  
    33.     public void SpawnObject(GameObject obj, Vector3 pos)
    34.     {
    35.         GameObject objToSpawn = PrefabUtility.InstantiatePrefab(obj) as GameObject;
    36.  
    37.         objToSpawn.transform.position = pos;
    38.         if (parent != null) objToSpawn.transform.parent = parent.transform;
    39.  
    40.         float scale = Random.Range(sizeRange.x, sizeRange.y);
    41.         float rotation = Random.Range(-180, 180);
    42.         float x = Random.Range(-7.5f, 7.5f);
    43.         float z = Random.Range(-7.5f, 7.5f);
    44.  
    45.         objToSpawn.transform.localScale = obj.transform.localScale * scale;
    46.         if (addRotation) objToSpawn.transform.eulerAngles = new Vector3(x, rotation, z);
    47.     }
    48.  
    49.     public void PlaceWater(Vector3 pos)
    50.     {
    51.         if (waters == null) waters = new List<GameObject>();
    52.  
    53.         GameObject objToSpawn = PrefabUtility.InstantiatePrefab(water) as GameObject;
    54.         waters.Add(objToSpawn);
    55.         pos.x = Mathf.RoundToInt(pos.x /30)*30 + waterOffset.x;
    56.         pos.y = waterYPos;
    57.         pos.z = Mathf.RoundToInt(pos.z / 30)*30 + waterOffset.y;
    58.    
    59.         objToSpawn.transform.position = pos;
    60.         objToSpawn.transform.parent = waterParent.transform;
    61.  
    62.     }
    63.  
    64.     public void DestroyAllObjects()
    65.     {
    66.         if (spawnedObjects.Count < 1) return;
    67.  
    68.         for (int i = 0; i < spawnedObjects.Count; i++)
    69.         {
    70.             Destroy(spawnedObjects[i]);
    71.         }
    72.  
    73.     }
    74.  
    75. }
    I know it's kinda sloppy, i just used this on a monobehavior because i couldn't figure out how to get it in an editor window lol. But the error is pretty annoying, everytime i build i have to comment out these 2 methods.

    The error disappears if i put it in the editor folder, but the inspector of the script on my gameobject becomes blank!
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    EDIT: please read my post that you quoted.
     
    Last edited: May 31, 2022
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,998
    What did you not understand at this sentence:

    This is a class that is defined in the Unity editor application itself. It does not exist at runtime. Yes, you can use it when you test it inside the editor, because you are inside the editor. However the Unity engine does not have this class so it can not be used at runtime. Runtime scripts can not use anything from the UntiyEditor namespace. That whole namespace MUST NOT be used at runtime because you CAN NOT use it, no matter how much you wish you could or how much you want you could. It's not possible.

    The "InstantiatePrefab" method creates a prefab instance inside the editor. At runtime you would just use UnityEngine.Object.Instantiate.
     
    Mashimaro7 and Kurt-Dekker like this.
  8. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
  9. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    That's all i needed to know lol. To everyone else, i wasn't trying to use it during build, I just needed to have it on a monobehavior and wasn't sure how to get it to build without commenting out the lines every time i build(I don't believe the script does anything in gameplay, it's a scene view only script lol). This is what i needed. Thank you
    Edit: Ah, at Kurt Decker, I didn't notice your last sentence that says you can add the #if UNITY_EDITOR lol
     
  10. XGgamerdev

    XGgamerdev

    Joined:
    Sep 13, 2022
    Posts:
    2

    It just doesnt work like it builds the thing but it doesnt work as expected
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The last step is critical because it may reveal how your expectations have diverged from the API reality.
     
    Bunny83 likes this.
  12. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,821
    Locking this thread as it's quite old.
     
    Kurt-Dekker likes this.
Thread Status:
Not open for further replies.