Hi guys
How would I replace one object with another at runtime ? I’m writing in C# by the way!
I have my object as a child to an empty game object ! How would I script it so that when I press a button it will change my “shield” to a “sword” ?
Could you please just provide the code to replace one object with another in C#
You can destroy an instanciate the object with the correct positions. Search by GameObject docs in the unity page.
You can make a GameObject the parent of another GameObject by making the transform of one into the parent of another, like so:
// gameObject1 is now the child of gameObject2
gameObject1.transform.parent = gameObject2.transform;
You can also activate and deactivate GameObject trees with the SetActive() function, and get keyboard input with Input.GetKeyDown().
You probably want to attach all your equipment to the character in the scene, activate the default equipment and deactivate all others at Start(), and when the key is pressed in the Update function activate the new equipment and deactivate the last equipment. Instantiating and Destroying memory is costly.