- Home /
Retrieve Object Prefab name
Hello, I am trying to retrieve object prefab name. I know that I can do that using:
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
If you have a GameObject variable and fill it with a reference to the prefab, you should be able to access it to get the name. Either as a public variable that can be assigned in the Editor before playing or at runtime via Resources.Load.
Answer by Serinx · Apr 29, 2018 at 10:32 PM
I think what you're looking for is Tags:
https://docs.unity3d.com/Manual/Tags.html
Tag all of your objects with the same tag, you can use FindGameObjectsWithTag to select them all.
GameObject[] dogs;
void Start()
{
dogs = GameObject.FindGameObjectsWithTag("dog");
}
void Update()
{
//Check for double click
//if doubleclick then select all the dogs
foreach (GameObject dog in dogs)
{
SelectionManager.Select(dog);
}
}
Obviously you'll need to flesh out the code a bit but you get the idea
Thank you for the response!
Yes I suppose I could do it that why, but in the future I might end up with hundreds of different objects (like: props, buildings, units and ect..) so this could easily end up being really inconvenient for me.
Currently I have an "ObjectID" script attached to every object, I could add / create a name variable and then access that variable and check if the name is the same between all the objects, but I'm not sure if that's the best way tho..
Your answer
![](https://koobas.hobune.stream/wayback/20220612161735im_/https://answers.unity.com/themes/thub/images/avi.jpg)