• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How do you add a child through code 2 Answers

turn off parent Gameobject based on a child game object that entered a Trigger? 2 Answers

Problem making object keep on RaycastHit point relative to another object 0 Answers

Can’t keep the position 0 Answers

Why does disabling an object eliminate its transform performance cost? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges