|
Post by britannis on Apr 13, 2016 15:13:03 GMT
Is this possible? I'm instantiating prefabs and want to check if an object in my hierarchy is a certain type of prefab. These are mostly static objects that don't impact gameplay besides have a collider so it feels weird to create an empty class for them and check that way. Any ideas?
|
|
|
Post by Admin on Apr 13, 2016 17:37:24 GMT
I don't think there is anything built-in to do this at run-time. A simple fix could be to check the name of the object - it should match with a "(Clone)" suffix. You could get the prefab name equivalent by doing something like: string testName = gameObject.name.Replace("(Clone)", "");
Out of curiosity, why do you need this ability? There may be a better architectural pattern, but it probably involves a new class. You might consider checking out my Object Pooling stuff, because the poolable items all have a key which can be used to identify the prefab it was created from: theliquidfire.wordpress.com/2016/02/24/poolers-part-1-of-4/
|
|
|
Post by Admin on Apr 13, 2016 17:50:28 GMT
I may not understand your question... did you mean you wanted to know what prefab was used to instantiate another prefab? If so, that was what I attempted to answer above.
If you were traversing your object graph and were curious if the object there was a prefab you placed at edit time (so that its name was blue in the hierarchy pane) then it won't have the suffix. By not already having a reference which you knew to be a prefab you also wouldn't have anything to compare against, so I don't know of a "good" solution for this.
|
|
|
Post by britannis on Apr 13, 2016 22:16:24 GMT
Ah, no. Basically I wanted to make sure an object existed at runtime but no more than one. Basically a singleton object but I felt that since the object only needed a box collider, it would be a bit extraneous to make a class for it. I ended up making an empty class for it anyway and just checking that.
How do you feel about an empty class or two in a project?
|
|
|
Post by Admin on Apr 14, 2016 1:39:20 GMT
If the class is a singleton it isn't empty. Also, you could make a "Loader" class into something reusable - you could give it an array of objects to instantiate instead of needing an empty class for each. To answer your question, as long as the purpose is clear I wouldn't mind having some simple classes. Sometimes I have what looks like an empty class just so I can have a concrete subclass of an abstract superclass - outside of the class definition it is empty.
|
|