- Home /
Question about instatiating prefab/clone character and how they work/interact with non prefeb buttons
Hello,
I have a question regarding how to control a prefab/clone that has been instatiated. I am making a game where a character prefab is instatiated or "spawned" when my player (that is not a prefab) gets hit by an enemy.
When my player gets hit a UI buttons pops up (isActive). It is basically a "start" button. When I click on this button I want the instatiated character prefab to do stuff (such as follow my player) as well as my non prefab player.
I am having issues because the button can't seem to "talk" to the instatiated prefab character. As in when I try to pass a variable to the class for the character it is always null because the buttons are not prefabs. Is the best method here to make everything a prefab so they can all pass varaibles to each other? My player, the second instatiated character and the button should all be prefabs or is there a better method?
Even when I try to find the button object (findObjectOfType<>) in the character class it comes up as null.
Answer by haizathaneefa · Jun 29, 2020 at 04:42 AM
Hello, you need to save your instantiated prefab into a gameobject. Something like
public GameObject gameObjectPrefab;
GameObject prefab;
void InstantiatePrefab()
{
gameObjectPrefab = Instantiate(prefab, transform.position, Quaternion.identity);
}
That way, you can access the property of said instantiated prefab.
Not a problem, I was in a similar situation before and got stuck for two weeks, lol. Happy coding!
Your answer
Follow this Question
Related Questions
How i can make a cube maze 0 Answers
Instantiate an object in a prefab? 1 Answer