Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by uglymug · Sep 17, 2016 at 08:12 PM · parentiskinematic

Changing parent at run-time and setting isKinematic=true

Hi,

I'm hoping someone can help me with a couple of issues please. Sorry to conflate 2 issues into one post but they apply to the same script. I have a cube that I want objects that collide with it to snap to it. The script is not yet complete but I am having 2 issues that are driving me mad:

Firstly the code successfully disables the script, sets isTrigger and clears useGravity on the object that collides with it (I can see this in the inspector at run-time) but it does not set isKinematic; the code lines for each operation are almost identical so I can't figure this out.

Secondly, I want to parent the object that collides with the cube to the cube itself. I have seen loads of examples on these forums and I have tried to replicate the solutions in my code but it just doesn't work for me. In an attempt to diagnose the problem I have reverted to just un-parenting the object (see code) but even that doesn't work! for information if I replace line 31 with just "transform.parent = null;" it successfully un-parents the cube. this doesn't help me but at least i know the syntax is correct.

any help would be most appreciated

Cheers

Ian

 using UnityEngine;
 using System.Collections;
 using VRTK;
 
 public class SnaptoMe : MonoBehaviour {
 
     private GameObject objinst;
     private VRTK_InteractableObject myscript;
     private BoxCollider mycollider;
     private Rigidbody myrigidbody;
 
     void OnCollisionEnter(Collision col) 
 
     {
         string objname = col.gameObject.name;
         GameObject objinst = GameObject.Find (objname);
     
 
         if (objname == "RedButton" || objname == "GreenButton" || objname == "BlueButton") 
         {
             
             myscript = objinst.GetComponent<VRTK_InteractableObject> ();
             mycollider = objinst.GetComponent<BoxCollider> ();
             myrigidbody = objinst.GetComponent<Rigidbody> ();
 
             myscript.enabled = false;
             mycollider.isTrigger = true;
             myrigidbody.isKinematic = true;
             myrigidbody.useGravity = false;
 
             objinst.transform.parent = null;
 
             //Destroy (objinst);
             //objinst.transform.parent = null;  //.transform;
 
             //objinst.transform.localPosition = Vector3.zero;
             //objinst.transform.localRotation = Quaternion.identity;
 
         }
             
     }
 }

Comment
Add comment · Show 1
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 uglymug · Sep 18, 2016 at 09:46 AM 0
Share

Hi again,

I have a bit of an update which is helping to home in where the issue might be for my parenting problem.

I have changed the code in line 31 to read "GameObject.Find ("BlueButton").transform.parent = transform;"

so effectively forcing a known object to be parented rather than using the one detected as part of the collision. Here is the interesting (frustrating) part. if I use the BlueButton object as the colliding object it wont allow BlueButton to be parented, if I use another object, such as RedButton or GreenButton, it will allow BlueButton to be parented. Is it because I am referencing BlueButton (objinst in my code) that stops me from changing its parent??

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by uglymug · Sep 18, 2016 at 08:29 PM

OK,

Found a work around. Posting it here in case someone else has the same problem in the future.

I still cannot parent the colliding object in code, I've had to instantiate a clone, destroy the original, parent it then rename it back to the original.

If anyone can explain why my original code did not work I'd be grateful.

Oh and it has also solved my isTrigger problem at the same time. Working code below:

 using UnityEngine;
 using System.Collections;
 using VRTK;
 
 public class SnaptoMe : MonoBehaviour {
 
     private GameObject objinst;
     private VRTK_InteractableObject myscript;
     private BoxCollider mycollider;
     private Rigidbody myrigidbody;
 
     void OnCollisionEnter(Collision col) 
 
     {
         //GameObject.Find ("BlueButton").transform.parent = transform;
 
         string objname = col.gameObject.name;
         GameObject objinst = GameObject.Find (objname);
         GameObject buttonclone;
     
 
 
 
         if (objname == "RedButton" || objname == "GreenButton" || objname == "BlueButton") 
         {
 
             buttonclone = Instantiate (objinst);
 
             myscript = buttonclone.GetComponent<VRTK_InteractableObject> ();
             mycollider = buttonclone.GetComponent<BoxCollider> ();
             myrigidbody = buttonclone.GetComponent<Rigidbody> ();
 
             myscript.enabled = false;
             mycollider.isTrigger = true;
             myrigidbody.isKinematic = true;
             myrigidbody.useGravity = false;
 
             buttonclone.transform.parent = transform;
             buttonclone.transform.localPosition = Vector3.zero;
             buttonclone.transform.rotation = transform.rotation;
             buttonclone.transform.Rotate (-90, 0, 0);
 
             Destroy (objinst);
             buttonclone.name = objname;
 
         }
             
     }
 }
Comment
Add comment · 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

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

68 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enable Multiple Box Collides From Parent Object 2 Answers

Child an Object to Another Objects Parent On Collision 1 Answer

iTween islocal still using object parent axes for MoveTo 0 Answers

Trying to set the parent of an instantiated object to another instantiated object 1 Answer

Keeping rigidbody-objects connected to their parents 2 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