Search Unity

Switch Parent of a gameobject while it's used for an attack (weapon)

Discussion in 'Editor & General Support' started by Imaginalex, Jun 27, 2018.

  1. Imaginalex

    Imaginalex

    Joined:
    Jan 29, 2018
    Posts:
    19
    Hello, i'm trying to have a character using his weapon in his hand only when he attacks, and rest / stay linked on his spine2 bone when he does not attack, but for those two different attacks it must be attached once to his right hand for attack "Fire1" and to his left hand for attack "Fire2"


    if (Input.GetKey(KeyCode.F))
    {
    Fire1();

    if (Input.GetButtonDown("Fire1"))
    {
    animator.SetTrigger("attackOne");
    GameObject.transform.Find("Group003").SetParent(gameobject.transform.Find("Group003").transform, false);
    GameObject.Find("Group003").transform.parent = GameObject.Find("Bip001 R Hand").transform;
    }

    and when i attack with the other button :


    if (Input.GetKey(KeyCode.E))
    {
    Fire2();

    if (Input.GetButtonDown("Fire2"))
    {
    animator.SetTrigger("attackOne");
    GameObject.transform.Find("Group003").SetParent(gameobject.transform.Find("Group003").transform, false);
    GameObject.Find("Group003").transform.parent = GameObject.Find("Bip001 L Hand").transform;
    }

    it returns this error :


    error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `SetParent' and no extension method `SetParent' of type `UnityEngine.GameObject' could be found. Are you missing an assembly reference?
     
  2. Imaginalex

    Imaginalex

    Joined:
    Jan 29, 2018
    Posts:
    19
    i think i need a "while" looking like this :


    using UnityEngine;
    using System.Collections;

    public class WhileLoop : MonoBehaviour
    {



    void Start ()
    {
    while (animator.attack1 = true)
    {
    GameObject.Find("Group003").transform.parent = GameObject.Find("Bip001 R Hand").transform;
    }
    if (animator.attack1 = false )
    {
    GameObject.Find("Group003").transform.parent = GameObject.Find("Spine2").transform;

    }

    }
    }
    }