- Home /
Question by
AverageCoder · Apr 02, 2017 at 09:25 PM ·
c#unity 5gameobjectparentboolean
Help needed on c# code which all of you will find basic apart from me
I am trying to make a gameobject (swordp) become a child of another gameobject (RightArm) if a bool in the animator parameter is true. I have tried but I am not sure what is wrong with the code here it is using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Sword_Parent : MonoBehaviour {
Animator anim;
public GameObject swordp;
public void SetParent (GameObject RightArm);
void Awake () {
anim = GetComponent <Animator> ();
}
void Update () {
var sword = anim.GetBool("sword_is_on");
if (sword == true)
swordp.transform.parent = RightArm.tranform;
if (sword == false)
}
}
code-pic.png
(36.1 kB)
Comment
Best Answer
Answer by Hellium · Apr 02, 2017 at 09:29 PM
What you need to do is the following :
public class Sword_Parent : MonoBehaviour {
// Drag & Drop the Sword Gameobject in the inspector
public Transform Swordp;
// Drag & Drop the Transform of the right arm in the inspector
public Transform RightArm ;
Animator anim;
void Awake () {
anim = GetComponent <Animator> ();
}
void Update () {
bool sword = anim.GetBool("sword_is_on");
if (sword)
Swordp.SetParent( RightArm ) ;
}
}