- Home /
Name of the prefab instantiated has clone suffixed
I have a player gameobject which when instantiated shows player(clone) in hierarchy view, but when I use the instantiated object name ("player") in the script it is not triggering the action.
In the script if I write other.name == ("Player(Clone))", then the behavior of the object is correct.
Please refer the below screenshots.
Answer by bhavinbhai2707 · Dec 08, 2019 at 09:56 AM
When you Instantiate an object to scene, Unity adds a suffix (clone) in order to differentiate it from original prefab or to just say,
"Hey! This player(clone), it's the object that you just spawned in scene"
So, when you use if(other.name == "Player")
it simply does not get executed, since its name is "Player(clone)"
There are two simple solutions.
When you Instantiate the Gameobject Player. Simply trim the clone word from its name.
GameObject go = Instantiate(Resources.Load("player") as GameObject); go.name = transform.name.Replace("(clone)", "").Trim();
Instead of checking for name, check for tag. Create and Add tag "Player" to your player prefab and simply check
if(other.CompareTag("Player")) { //Your code here }
Your answer
Follow this Question
Related Questions
Unity loading prefabs difficulties. optimization 1 Answer
Is their any way to save a particle system when it's not instantiated yet? 2 Answers
2D character not shooting in correct direction 1 Answer
How can I save multiple instances of a single prefab? 1 Answer
How to Instatiate a particle system on a moving object 2 Answers