- Home /
sending message to bullet (javascript)
In my gun's script my projectile prefab is a gameobject. Then in turn the instantiated bullet is a gameobject(i think). But i cant find, for the life of me, how to send a message to a gameobject... here is my code:
#pragma strict
var ammo : int;
var maxAmmo : int;
var charge : float;
var damage : int;
var force : int;
var projectile : GameObject;
var fireRate = 0.5;
var nextFire = 0.0;
var accuracy : int;
var loaded : boolean;
function Start () {
}
function Update ()
{
if (Input.GetButtonDown ("Fire1") && Time.time > nextFire && ammo > 0) //make bullet
{
ammo--;
var shot = Instantiate (projectile, transform.position, Quaternion.Euler(Vector3(transform.eulerAngles.x+Random.Range(0,accuracy), transform.eulerAngles.y+Random.Range(0,accuracy), transform.eulerAngles.z+Random.Range(0,accuracy))));
loaded = true;
}
if(Input.GetButton ("Fire1") && loaded) //charge while holding mouse
{
// send this message to shot somehow.
// SendMessage("Charge", SendMessageOptions.DontRequireReceiver);
}
if (Input.GetButtonUp ("Fire1") && loaded) //release bullet
{
nextFire = Time.time + fireRate;
loaded = false;
}
}
also my instantiate line is massive because im tryign to simulate accuracy with the transform.eulerAngles.(xyz)+Random.Range(0, accuracy). if anyone knows a simpler way id apreciate the help.
Are you having a problem using Send$$anonymous$$essage? Your question isn't very clear.
I just don't know the syntax for sending a message to a gameobject. in my script the varible shot it a gameobject that was created after my mouse button is clicked. while the mouse is being held i need to send a message to the bullet to call its charge function. but shot.Send$$anonymous$$essage("Charge", Send$$anonymous$$essageOptions.DontRequireReceiver) isnt working.
That's because shot is only going to exist for one frame, and only inside the { } of the if.
Answer by whydoidoit · Apr 29, 2013 at 06:30 AM
So either hold on to the value of shot in a variable attached to the script, rather than a local variable or turn the whole thing into a coroutine.
#pragma strict
var ammo : int;
var shot : GameObject;
var maxAmmo : int;
var charge : float;
var damage : int;
var force : int;
var projectile : GameObject;
var fireRate = 0.5;
var nextFire = 0.0;
var accuracy : int;
var loaded : boolean;
function Start () {
}
function Update ()
{
if (Input.GetButtonDown ("Fire1") && Time.time > nextFire && ammo > 0) //make bullet
{
ammo--;
shot = Instantiate (projectile, transform.position, Quaternion.Euler(Vector3(transform.eulerAngles.x+Random.Range(0,accuracy), transform.eulerAngles.y+Random.Range(0,accuracy), transform.eulerAngles.z+Random.Range(0,accuracy))));
loaded = true;
}
if(Input.GetButton ("Fire1") && loaded) //charge while holding mouse
{
// send this message to shot somehow.
shot.SendMessage("Charge", SendMessageOptions.DontRequireReceiver);
}
if (Input.GetButtonUp ("Fire1") && loaded) //release bullet
{
nextFire = Time.time + fireRate;
loaded = false;
}
}
or like this:
function Shoot()
{
ammo--;
var shot = Instantiate (projectile, transform.position, Quaternion.Euler(Vector3(transform.eulerAngles.x+Random.Range(0,accuracy), transform.eulerAngles.y+Random.Range(0,accuracy), transform.eulerAngles.z+Random.Range(0,accuracy))));
while(Input.GetButton ("Fire1"))
{
shot.SendMessage("Charge", SendMessageOptions.DontRequireReceiver);
yield;
}
nextFire = Time.time + fireRate;
}
function Update()
{
if (Input.GetButtonDown ("Fire1") && Time.time > nextFire && ammo > 0)
{
Shoot();
}
}
ohhh man it was a scope issue i see, thanks. can't believe i didn't notice.
No problem - happens to us all :D Can you tick the answer for me?
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Rapid fire Help 1 Answer
AddForce is not working 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers