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.
1 Answer
1
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();
}
}
Are you having a problem using [SendMessage][1]? Your question isn't very clear. [1]: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html
– iwaldropI 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.SendMessage("Charge", SendMessageOptions.DontRequireReceiver) isnt working.
– hardmode2236That's because shot is only going to exist for one frame, and only inside the { } of the if.
– whydoidoit