Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JellyToTheMax · Mar 05, 2013 at 03:33 PM · c#rigidbodychilditemunparenting

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>();
             
             
         
             //}
         }
     //}
     //}
 //}
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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>();
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GeorgeRigato · Mar 05, 2013 at 04:25 PM 0
Share

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?

avatar image JellyToTheMax · Mar 05, 2013 at 05:21 PM 0
Share

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 :]

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to control parenting in multiple functions 1 Answer

Prevent Rigidbody from Climbing Steep Slopes using Forces (C#) 0 Answers

how to keep a "ship" over a platform or rigidbody? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges