Here is a simple list of objects you ordinarily access in Unity:
gameObject
transform
rigidbody
collider
renderer
So each of these is an object. As a matter of fact, almost everything in Unity is an object, linked in some form or fashion. A transform is the object of a gameObject that controls position, rotation and various other things. A rigidbody is actually a component. (A component is an object that is attached to another object)
So to help you go down the right direction… Everything in Unity is an object. This includes scripts.
So lets look at this script:
//myCar.cs
using UnityEngine;
using System.Collections;
public class myCar : MonoBehaviour {
public Rigidbody bodyhit = null;
}
We can see the script is named “myCar” and extends the MonoBehaviour class. All classes are effectively un-created objects. So in order to create the object we need to attach it to something.
lets make a cube, with 4 sphere on it’s corners, and we will attach this script to the cube. Lets just call the cube… “car” just for the fun of it.
The cube will need a rigidbody, so we need to attach one to it.
Now, lets look at the spheres… and for the fun of it lets call them "wheel"s. Each “wheel” needs a collider, so lets use the WheelCollider. (imagine that) Next, lets get some script on each wheel.
//myWheel.cs
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void FixedUpdate() {
Vector3 forward = collider.attachedRigidbody.transform.forward;
collider.attachedRigidbody.AddForceAtPosition(forward, transform.position);
}
}
Again, this script is called “myWheel” and extends the MonoBehaviour class. And, again, we need to attach it to each wheel to make it work.
In the case of the wheel, every physics cycle (FixedUpdate) each wheel is going to apply force to the car, but only at the point where the wheel is. This is going to mimic what real wheels do.
OK, what makes this special, and why do we care? Well, in the FixedUpdate method, we are creating a vector from a collider. How do we do this? Objects… First, the collider is an object. It has certain methods that help us handle things. One of those things is a property called “attachedRigidbody”. This is a pointer to an object… which is the rigidbody that we are currently attached to. This rigidbody is somewhere in the hierarchy of this object. Generally it is on the same object, or just above it in the stack. This means that by using collider.attachedRigidbody, we gain access to the Rigidbody that is held there.
Now… Rigidbody is much like the collider in that it has lots of pointers to other objects, as well as it’s own set of properties and methods. One of these pointers is to a thing called “transform”. This allows us access to the Transform attached to the same object that the rigidbody is attached to. This is the positioning of the car’s body. The Transform also has its own set of stuff… and we continue on…
OK, now, lets consider that the script “myCar.cs” is an object. The script can be attached by dragging it to the model or inspector to attach it because it extends the MonoBehaviour class. (Yes, this is VERY important)
Now, because it is attached to the car’s GameObject, we can access it by using some of the methods in the GameObject … object.
The exact method is "GetComponent(). To access it, we need to fill out the generic …
ScriptName other = gameObject.GetComponent<ScriptName>();
So, now lets answer your question. “How do you access a property, component or method of a class, from outside the class, IF that class is a MonoBehaviour derived class?”
Rigidbody bodyhit = gameObject.GetComponent<myCar>().bodyhit;
And better still…
Rigidbody bodyhit = collider.attachedRigidbody.gameObject.GetComponent<myCar>().bodyhit;
Now, you can see where object to object, we get a link from the current active collider, to the rigidbody that it is attached to, then to the game object it is attached to, then finally to the script we are using and lastly to the property “bodyhit”.
Hope this helps.