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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Retrieve Object Prefab name

Discussion in 'Scripting' started by Rukas90, Apr 29, 2018.

  1. Rukas90

    Rukas90

    Joined:
    Sep 20, 2015
    Posts:
    167
    Hello,
    I am trying to retrieve object prefab name. I know that I can do that using:
    Code (CSharp):
    1. PrefabUtility.GetPrefabParent(gameObject).name
    but the problem is that it only works inside editor. I'm working on the 'SelectionManager' in my game and right now I want to make double click on an object and select multiple same objects method, but I first want to somehow tag all the objects so that when I double click on a curtain object it would only select objects with the same type / tag. I thought about making an enum, but getting a name is much more simple and convenient.
    Are there any ways in getting the prefab name outside the editor? Also maybe it's possible to somehow get the original gameObject name without counter tag?

    Or are there any better way's in making something like this? Thank you
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Finding objects by name is very slow. If you are creating them, why not add them to a list or array?
     
  3. Rukas90

    Rukas90

    Joined:
    Sep 20, 2015
    Posts:
    167
    Thank you for the response!
    Yes of course, I do not find objects by name, what I do is at the start of the game I make all the objects that use selectable interface add themselves to the selectableSceneObjects list, then everytime I drag select or double click I loop through that selectableObjects list and check if object in that list is inside that rect, if it is I select this object.
    But what I want is to somehow tag all these objects. I could add a name variable and then access that variable and check if the name is the same, but I'm not sure if that's the best way tho.,
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You could spawn them as the prefab type. For instance:
    Instantiate(enemy) as enemy;
    Then use FindObjectOfType(enemy);
    or if you are cycling through and array:
    if(object.typeof(enemy){}

    Caveat here is I haven't actually done this, but it should work I would think. Not sure if the syntax is right either, but it would be something like that. I have to have Visual Studio.
     
    Last edited: Apr 30, 2018