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;

		}
			
	}
}

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;

		}
			
	}
}