I can't describe the problem as a question, Rigidbody problem.


I have problem. Im using a raycast to pick up objects. Im saving the raycasthit in a gameobject variable, When i wan’t to disable the rigidbody from the gameObject variable, it won’t work cause the gameObject var does not have a rigidbody, it just gives me errors,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerSys : MonoBehaviour {

	public GameObject activeItemBP;
	public GameObject activeItem;
	public GameObject backPack;

	public GameObject takenObject;

	public Camera cam;
	public int range;

	private GameObject TheHitObject;

	// Update is called once per frame
	void Update () 
		RaycastHit hit;

		if (Input.GetKeyDown (KeyCode.E)) 
			if (Physics.Raycast (cam.transform.position, cam.transform.forward, out hit, range)) 
				TheHitObject = hit.collider.gameObject;


					activeItem = TheHitObject;
					TheHitObject.transform.parent = activeItemBP.transform;
					TheHitObject.transform.position = activeItemBP.transform.position;
                                     /* Here is where i want toTheHitObject.GetComponent<Rigidbody>
                                     ().SetActive(false); but it wont work, its not important that i deactivate the rigidbody 
                                     exactly like that! */



You ca’t disable rigidbody with SetActive() and even with enabled = false. You can destroy rigidbody or set isKinematic = true. And are you sure that your pickable objects have attached rigidbody component?

It is possible that you hit with the raycast a game object that has a Collider but not a Rigidbody. That is why it is necessary to check the result of TheHitObject.GetComponent<Rigidbody>() before you call anything on it. However, as gordiri pointed out, a Rigidbody does not haveSetActive(), only GameObjects have. To disable a Rigidbody you need to make it kinematic:

Rigidbody hitObjectRigidBody = TheHitObject.GetComponent<Rigidbody>();
if (hitObjectRigidBody != null) { hitObjectRigidbody.isKinematic = true; }

Another thing, unless you are using an older version of Unity, instead of transform.parent = ... use transform.SetParent(...). It is much better approach. Just be aware that SetParent() has a second parameter which you usually want to set to true.

Finally, a minor style thing, I would name TheHitObject as theHitObject, because the convention in C# is that only types (classes, structs, enums, delegate types) and function names start with capital letter, variables almost always start with lower-case.