- Home /
Find Specific Child Then Un-Parrent It and add RigidBody For Unequiping items. c#
I'm trying to Create a script that will allow a player to walk up to an item and on pressing a button it will equip the nearest object and put it in the Mountr(right mount) position. If an item is already equipped in that slot then this script below will find child 0 the first child and add a rigid body to it then Un-Parrent it. This is the part im having trouble with, which is adding the rigid body, i keep getting errors like cannot convert transform to gameobject. I don't want to just get this script working i want to learn what im doing wrong and how to avoid this in the future. If you can be detailed in what my mistakes are i would be greatly appreciative.
public Transform Mountr;
public Transform Mountl;
public GameObject Item;
private GameObject Unequip;
void Update () {
if(Input.GetKeyUp(KeyCode.F)) { //and the player right clicks on me
//if(MainHand = true) { // and if this is a main hand weapon
GameObject go; //equip the var weapon in main hand slot
go = Instantiate(Item,Mountr.position, Mountr.rotation) as GameObject;
go.transform.parent=Mountr;
//transform.localPosition = Vector3.zero;
//transform.localRotation = Quaternion.identity;
//Weapon Weapon = Item.GetComponent<Properties>();
//Item = GameObject.Find("Item");
if( Mountr.transform.childCount > 0 ) {
//Destroy( Mountr.transform.GetChild( 0 ).gameObject );
Unequip = Mountr.transform.GetChild( 0 );
Unequip.gameObject.AddComponent(Rigidbody);
//Mountr.Transform.Child.AddComponent( "Mesh Collider" );
//Mountr.Transform.Child.AddComponent(Rigidbody);
Mountr.transform.GetChild( 0 ).parent = null;
}
}
Below is my full script just in case anyone else wants to put something similar in their game and are having problems with some parts of this as well. This script is heavily commented and has a lot of disabled features for the main reason that this is a big work in progress and by far my biggest project thus far. I'm only 2 months into learning unity and scripting.
using UnityEngine;
using System.Collections;
public class Weild : MonoBehaviour {
//public GameObject Weapon;
//public GameObject Glow;
//public bool MainHand;
//public bool OffHand;
//public int maxDistance = 6;
public Transform Mountr;
public Transform Mountl;
public GameObject Item;
private GameObject Unequip;
void Start () {
//Player = (GameObject) GameObject.FindWithTag ("Player");
//GameObject[] Item = GameObject.FindGameObjectsWithTag("Item").Transform;
//objCamera = (GameObject) GameObject.FindWithTag ("MainCamera");
//gos = GameObject.FindGameObjectsWithTag(tagName);
//if (gameObject.tag == "Player") { thisIsPlayer = true; }
}
void Update () {
if(Input.GetKeyUp(KeyCode.F)) { //and the player right clicks on me
//if(MainHand = true) { // and if this is a main hand weapon
GameObject go; //equip the var weapon in main hand slot
go = Instantiate(Item,Mountr.position, Mountr.rotation) as GameObject;
go.transform.parent=Mountr;
//transform.localPosition = Vector3.zero;
//transform.localRotation = Quaternion.identity;
//Weapon Weapon = Item.GetComponent<Properties>();
//Item = GameObject.Find("Item");
if( Mountr.transform.childCount > 0 ) {
//Destroy( Mountr.transform.GetChild( 0 ).gameObject );
Unequip = Mountr.transform.GetChild( 0 );
Unequip.gameObject.AddComponent(Rigidbody);
//Mountr.Transform.Child.AddComponent( "Mesh Collider" );
//Mountr.Transform.Child.AddComponent(Rigidbody);
Mountr.transform.GetChild( 0 ).parent = null;
}
}
}
//public void Wield(Transform parent) {
//Mountr = get players right weapon mount through
//higheriachical call like (/player/body/hand/left_hand/mount");
//Mountl = get players left weapon mount Or any other means
//to select the correct mount of the player who clicks on the item that holds this script.
//finds all local players
//float distance = Vector3.Distance(Item.transform.position, transform.position);
//defines The distance var used in the next line
//if (distance < maxDistance) { //if i am close enough to the target
//if(Input.GetKeyUp(KeyCode.F)) { //and the player right clicks on me
//if(MainHand = true) { // and if this is a main hand weapon
//GameObject go; //equip the var weapon in main hand slot
//go = Instantiate(Item,Mountr.position, Mountr.rotation) as GameObject;
//go.transform.parent=transform;
//transform.localPosition = Vector3.zero;
//transform.localRotation = Quaternion.identity;
//Weapon Weapon = Item.GetComponent<Properties>();
//Item = GameObject.Find("Item");
//}
//else if(OffHand = true) { // otherwise if it is an off hand weapon
//GameObject go; //equip the var weapon in offhand slot
//go=Instantiate(Weapon,Mountl.position, Mountl.rotation) as GameObject;
//transform.localPosition = Vector3.zero;
//transform.localRotation = Quaternion.identity;
//Weapon Weapon = Item.GetComponent<Properties>();
//}
}
//}
//}
//}
Answer by robertbu · Mar 05, 2013 at 04:03 PM
I appreciate you indicated the line the error is on with a comment (many posters don'). For people attempting to answer the question, including the full error message is also very beneficial. In this case your error is:
Assets/_Project/Scripts/Debugging07.cs(41,19): error CS0029: Cannot implicitly convert type UnityEngine.Transform' to
UnityEngine.GameObject'
The issue is that transform.GetChild( 0 ) returns a transform, not a game object. By luck (or good Unity design?), transforms contain a gameObject, so you can fix your problem like this:
Unequip = Mountr.transform.GetChild( 0 ).gameObject;
On the next line you also have an error. You can change Rigidbody to "Rigidbody" in quotes, or you can use this form of AddComponent:
Unequip.gameObject.AddComponent<Rigidbody>();
Just a suggestion: Why don't you make your items already with a rigidbody, but no gravity and kinematic, and then change both flags, making it to respond gravity and removing kinematic?
I am very sorry, i will do that in the future. It was very careless of me to not post the error mssg. Also thanks so much! it works!!! Now back to improving the script myself :]