I'm trying to read a public float called shootForce contained in a monoBehaviour class called player001controls using a monoBehaviour script called missileControls with cSharp.
This executes, but when I fire a missile I get this error :
Removing the 'new' from
gives the error
I want to be able to access the float shootForce at runtime, read from player001controls (or any other script).
OK, heres my solution (final)
To create an instance of a MonoBehaviour class at runtime you have to use AddComponent because all components can't exist on it's own. They always need to be attached to a GameObject. If you want to create a script instance on the same GameObject where this script is attached to just use:
Ohh a little sidenote: classnames always should start with a capital letter. It get's very very difficult to read code where you mix typenames with variable names. That are some basic programming conventions.
answered Nov 21 '11 at 12:13 AM
I think you you make a prefab out of the player001controls and Instantiate it, you'll be good to go.
public GameObject player001PreFab; // set this with inspector
GameObject gObj = Instantiate(player001PreFab, Vector3.zero, Quaternion.identity);
missileControl = (player001controls)gObj.GetComponent(typeof(player001controls));
I recommend that you start by reading this:
The above link explains some of the basic concepts of how scripts interact in Unity.
answered Nov 20 '11 at 09:02 PM