- Home /
script doesn't do anything
i made a script but nothing happens, i might have just made some simple mistake because i havent used unity in about 4 months
var health = 999;
var bullet1 : GameObject;
var bullet2 : GameObject;
var player : Transform;
function Start () {
Invoke("Shoot",3);
}
function OnCollisionEnter () {
health -= 1;
}
function Shoot () {
if(health >= 800){
if(health <= 1000){
transform.LookAt(player);
Instantiate(bullet1,transform.position,transform.rotation);
yield WaitForSeconds(0.01);
Instantiate(bullet2,transform.position,transform.rotation);
yield WaitForSeconds(0.02);
Instantiate(bullet1,transform.position,transform.rotation);
yield WaitForSeconds(0.04);
Instantiate(bullet2,transform.position,transform.rotation);
yield WaitForSeconds(0.08);
Instantiate(bullet1,transform.position,transform.rotation);
yield WaitForSeconds(0.16);
Instantiate(bullet2,transform.position,transform.rotation);
yield WaitForSeconds(0.24);
Instantiate(bullet1,transform.position,transform.rotation);
yield WaitForSeconds(0.32);
Instantiate(bullet2,transform.position,transform.rotation);
Invoke("Shoot",0.5);
}
}
}
what i want to do is shoot a stream of bullets at the player but no bullets appear, there are no error messages and the bullets and player are assigned to the object correctly
I am confused. This looks like a weapon controller yet it has health on it? Anyway, check your bullet gameobject. Also you are delaying the bullet creation in tiny incremental amount. You could use a for loop to do the same thing. Also, Invoke calls the function after x seconds. First try to instantiate one at a time.
Also make sure it is attached to a game object.
i wanted to have it like that so that when the enemy was between 800 and 600 health i would make a different sequence
ill see if changing shoot to start makes it shoot
you could use state machine like normal, angry, frenzy type modes which are seen in a couple of games, but I digress. Anyway, just check if it is the game object not being created or the script. Also, if you even have it attached to an object, sometimes we miss it and check if it is enabled as well.
i changed function shoot to function start and it shot once, also i updated to unity 4 and now i get an error message with a white speech bubble thing telling me that the function shoot couldn't be called
yes i googled it
Answer by sam32x · Jan 25, 2013 at 03:25 AM
i removed the waitforseconds thing because that was the only thing there that i hadn't used before and it started working but then it stopped again for no reason.
here is my script now
var health = 5000;
var bullet1 : GameObject;
var bullet2 : GameObject;
var bullet1f : GameObject;
var bullet2f : GameObject;
var player : Transform;
function Start () {
Invoke("Shoot",3);
}
function OnTriggerEnter () {
health -= 1;
}
function Shoot () {
if(health >= 5000){
if(health <= 4750){
transform.LookAt(player);
Instantiate(bullet1,transform.position,transform.rotation);
Instantiate(bullet2,transform.position,transform.rotation);
Invoke("Shoot",0.5);
}
}
if(health >= 4750){
if(health <= 4500){
transform.LookAt(player);
Instantiate(bullet1f,transform.position,transform.rotation);
Instantiate(bullet2f,transform.position,transform.rotation);
Invoke("Shoot",0.5);
}
}
if(health >= 4500){
if(health <= 4250){
transform.LookAt(player);
transform.Rotate(Vector3.left * 30);
Instantiate(bullet1,transform.position,transform.rotation);
transform.Rotate(Vector3.right * 15);
Instantiate(bullet1,transform.position,transform.rotation);
transform.Rotate(Vector3.right * 15);
Instantiate(bullet1,transform.position,transform.rotation);
transform.Rotate(Vector3.right * 15);
Instantiate(bullet1,transform.position,transform.rotation);
transform.Rotate(Vector3.right * 15);
Instantiate(bullet1,transform.position,transform.rotation);
transform.Rotate(Vector3.left * 30);
Invoke("Shoot",0.5);
}
}
}
Something is telling me that maybe there is a problem with Invoking a function in itself, but not completely sure.
After googling some stuff I came across http://answers.unity3d.com/questions/376748/invokerepeating-stops-suddenly.html
Check out http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html see if that helps.
InvokeRepeating didn't work and neither did changing Invoke("Shoot",3); to Shoot();
Could you post the code, because the one just above says you are still using Invoke.
var health = 5000;
var bullet1 : GameObject;
var bullet2 : GameObject;
var bullet1f : GameObject;
var bullet2f : GameObject;
var player : Transform;
function Start () {
Shoot();
}
function OnTriggerEnter () {
health -= 1;
}
function Shoot () {
if(health >= 5000){
if(health <= 4750){
transform.LookAt(player);
Instantiate(bullet1,transform.position,transform.rotation);
Instantiate(bullet2,transform.position,transform.rotation);
Invoke("Shoot",0.5);
}
}
if(health >= 4750){
if(health <= 4500){
transform.LookAt(player);
Instantiate(bullet1f,transform.position,transform.rotation);
Instantiate(bullet2f,transform.position,transform.rotation);
Invoke("Shoot",0.5);
}
}
if(health >= 4500){
if(health <= 4250){
transform.LookAt(player);
transform.Rotate(Vector3.left * 30);
Instantiate(bullet1,transform.position,transform.rotation);
transform.Rotate(Vector3.right * 15);
Instantiate(bullet1,transform.position,transform.rotation);
transform.Rotate(Vector3.right * 15);
Instantiate(bullet1,transform.position,transform.rotation);
transform.Rotate(Vector3.right * 15);
Instantiate(bullet1,transform.position,transform.rotation);
transform.Rotate(Vector3.right * 15);
Instantiate(bullet1,transform.position,transform.rotation);
transform.Rotate(Vector3.left * 30);
Invoke("Shoot",0.5);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613091539im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Invoke not working from Start() ? 1 Answer
how to make it so i can see a raycast 2 Answers
Control amount of bullets 2 Answers
Invoke is not working properly. 0 Answers
simple Shooting in C# script please 1 Answer