- Home /
Script Error!!
I really do not understand what is wrong with my script, I have an error saying expecting ( , found Shoot. But I dont have a clue what I have done wrong can anyone help me?
function Update (){
if(Input.GetButtonDown("Fire1")){
Shoot();
}
var shootSound : AudioClip;
var bloodPrefab : Transform;
var sparksPrefab : Transform;
var range = 500;
function Shoot(){
if(Physics.Raycast(transform.position, transform.forward, hit, range)){
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(hit.collider.tag == "Enemy"){
if(bloodPrefab){
Instantiate(bloodPrefab, hit.point, rot);
}
hit.collider.gameObject.SendMessage("ApplyDamage", 25, SendMessageOptions.DontRequireReceiver);
}else{
if(sparksPrefab){
Instantiate(sparksPrefab, hit.point, rot);
}
}
}
}
}
you have got all your variables inside the Update() function... try moving them to the very top of your script!!!!
Answer by Grady · Dec 22, 2011 at 09:09 PM
Here you go, I think your error was that you had your curly brackets for closing your Update()
function after the Shoot()
function, plus I think you had an extra one there in the end as well!!!!!! There were also a couple of errors with the raycast, so I updated that too!!!! I fixed up your code for you and it should work now!!!!! I also indented it, so it is a bit easier to read!
var shootSound : AudioClip; var bloodPrefab : Transform; var sparksPrefab : Transform; var range = 500; var hit : RaycastHit;
function Update (){ if(Input.GetButtonDown("Fire1")){ Shoot(); } }
function Shoot(){ var fwd = transform.TransformDirection (Vector3.forward); if(Physics.Raycast(transform.position, fwd, hit, range)){ var rot = Quaternion.FromToRotation(Vector3.up, hit.normal); if(hit.collider.tag == "Enemy"){ if(bloodPrefab){ Instantiate(bloodPrefab, hit.point, rot); } hit.collider.gameObject.SendMessage("ApplyDamage", 25, SendMessageOptions.DontRequireReceiver); } else{ if(sparksPrefab){ Instantiate(sparksPrefab, hit.point, rot); } } } }
It should work now, but if not, then just post back
your welcome, I also switched your raycast method around, i'm not sure if it was working for you, but when i put your script into my unity it came up with some errors with the hit variable, so i updated your raycast error at the top of the shoot() function!!!
Glad I could help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613062319im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Null in GetValidMethodInfo 0 Answers
A node in a childnode? 1 Answer
press play, game objects get deleted, game breaks. 1 Answer
Targetting error please help 0 Answers