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;
}
}
}
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??
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;
}
}
}
Your answer
Follow this Question
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