- Home /
Different prefab getting instantiated?
I'm trying to instantiate a certain prefab whenever the player drops an item, but for some reason it always end up being one certain prefab, a different one every time.
public void DropItem()
{
Instantiate(this.item.Object, player.transform.position, player.transform.rotation);
}
I'm not sure why it's instantiating a different prefab, because the script clearly should make it instantiate whatever game object that the script is attached to.
Answer by RustyCrow · Aug 17, 2018 at 10:56 PM
Hmm this part is confusing to me "but for some reason it always end up being one certain prefab, a different one every time." looking at the code you are spawning whatever is in item.object.
I will assume the correct Drop() is being called, so that leaves us with what is in the item.Object. Are you sure that you have "Dragged " the correct Prefab in Object slot or are you trying to spawn a clone of the game object the script is on ?
" script clearly should make it instantiate whatever game object that the script is attached to." Hmm if you want to spawn a clone of the gameobject that this script is on then gameobject should be used.
Here is what i think is happning you are trying to spawn a Prefab(location folder) but you keep spawning the gameobject in the scene because you have set the value(item.object) to this.gameObject. Or you are misunderstanding something about prefabs and GameObjects.
In the end the Code is solid its spawning whatever you put in item.object.So you need to trace back what is happening to that value before instantiate, i suggest Debug.log(item.Object.name) right before instantiate() see if it matches the Prefab or GameObject(?) name.
It works! your answer helped me in a way, I ended up moving the method to the item script to call the Object variable directly, and that seemed to do the trick. thank you so much!
Your answer
Follow this Question
Related Questions
Instantiating gameObjects and prefabs issues. 0 Answers
Create varying numbers of gameObjects/prefabs in sceneview? 0 Answers
Checking if a gameobjects active in your heirarchy. 2 Answers
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers
GetComponent vs AddComponent 3 Answers