- Home /
How to cycle weapons and parent it?
I am trying to make a simple weapons cycle for a simple fps I was making for fun. I decided for weapon cycle to try to instantiate the prefab and parent it to where it belongs (Not sure if this is best way to do a weapons cycle if not let me know). However I can't seem to instantiate it correctly. I am also stuck on how I would parent it. Thanks in advance.
using UnityEngine;
using System.Collections;
public class WeaponSwitch : MonoBehaviour {
public GameObject Magnum;
public GameObject Player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("1"))
{
GameObject obj;
obj = (GameObject)Instantiate(Magnum, Player.transform.position, Player.transform.rotation);
}
}
}
Answer by thomasindustry · Jun 11, 2014 at 08:32 PM
To parent an object via script, change its transform.parent to the transform of the object you want it to be attached to.
Instead of instantiating every time the button to cycle weapons is pressed, it would be more efficient to instantiate them all at the start, and then just enable/disable the objects as you cycle though.
Your answer
Follow this Question
Related Questions
Why say Unity that 320* (1 / 320) = 0? 5 Answers
how to add package manager ??? Unity Pro 2018.3.2f1 0 Answers
Regarding ScreenToWorldPoint y 0 1 Answer
How to drag game object in fixed path? 0 Answers