- Home /
What is wrong with this catapult script?
Hi can anyone help me please. I've got two errors with this particular script. The error messages are:
NullReferenceException CatapultMainScript.Start () (at Assets/Scripts For Project/CatapultMainScript.js:24)
NullReferenceException CatapultMainScript.Update () (at Assets/Scripts For Project/CatapultMainScript.js:33)
The script is:
var grenade : GameObject; var throwThis : int = 3200; var rotspeed : int = 10;
private var placeHolder : Transform; private var Launcher : Transform; private var target : GameObject; private var cRight : Transform; private var cLeft : Transform; private var firingForm : float; private var distance : float; private var originalPos : Vector3; private var originalRotation : Quaternion; private var originalThrow : int; private var originalDist : float;
function Start(){
placeHolder = transform.Find("Catapult Base/Catapult Launcher/Holder");
target = GameObject.Find("target");
cRight = target.transform.Find("CubeR");
cLeft = target.transform.Find("CubeL");
Launcher = transform.Find("Catapult Base/Catapult Launcher");
firingForm = Vector3.Distance(placeHolder.transform.position, target.transform.position);
originalDist = firingForm;
originalPos = target.transform.position;
originalRotation = Launcher.transform.rotation;
originalThrow = throwThis;
}
function Update(){
placeHolder.transform.LookAt(target.transform);
if(Input.GetAxis("Mouse X") > 0 && cRight.renderer.isVisible){
Launcher.transform.Rotate(0, Time.deltaTime * (rotspeed * -1), 0);
target.transform.Translate(Time.deltaTime * (rotspeed * 2), 0, 0);
firingForm = Vector3.Distance(placeHolder.transform.position, target.transform.position);
}
if(Input.GetAxis("Mouse X") < 0 && cLeft.renderer.isVisible){
Launcher.transform.Rotate(0, Time.deltaTime * rotspeed, 0);
target.transform.Translate(Time.deltaTime * (rotspeed * 2) * -1, 0, 0);
}
if(Input.GetAxis("Mouse Y") > 0 && target.transform.position.z < 913){
firingForm = Vector3.Distance(placeHolder.transform.position, target.transform.position);
target.transform.Translate(0, 0, (Time.deltaTime * (rotspeed * 2)));
}
if(Input.GetAxis("Mouse Y") < 0 && target.transform.position.z > 773){
firingForm = Vector3.Distance(placeHolder.transform.position, target.transform.position);
target.transform.Translate(0, 0, (Time.deltaTime *(rotspeed * 2) * -1));
}
if(Input.GetKey("space")){
Launcher.transform.rotation = originalRotation;
target.transform.position = originalPos;
throwThis = originalThrow;
}
if(Input.GetButtonDown("Fire1")){
firebomb();
}
}
function firebomb() {
animation.CrossFade("fire");
yield WaitForSeconds(1);
throwThis = originalThrow - ((originalDist - Vector3.Distance(placeHolder.transform.position, target.transform.position)) *35);
var clone = Instantiate(grenade, placeHolder.transform.position, placeHolder.transform.rotation);
clone.rigidbody.AddRelativeForce(Vector3.forward * (throwThis));
} Thank you for your help.
Answer by aldonaletto · Dec 31, 2011 at 07:21 PM
Unity probably can't set one of the variables placeholder, Launcher or target because the respective objects were not found:
1- If there's no object named "target" in the scene, target will not be set;
2- The object that has this script must have a child named "Catapult Base", which must have a child named "Catapult Launcher" to be assigned to the variable Launcher;
3- The child object "Catapult Launcher" must have a child object named "Holder" to be assigned to the variable placeHolder.
Check if these objects exist and if their names are set as above.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062539im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Flipping textures 0 Answers
How do I invert the Y axis in Penelope tutorial? 3 Answers
Rotating a Sphere with the mouse? 1 Answer