• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hardmode2236 · Apr 29, 2013 at 05:35 AM · javascriptshootingbulletmessage

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Apr 29, 2013 at 05:38 AM 0
Share

Are you having a problem using Send$$anonymous$$essage? Your question isn't very clear.

avatar image hardmode2236 · Apr 29, 2013 at 06:20 AM 0
Share

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.

avatar image whydoidoit · Apr 29, 2013 at 06:25 AM 0
Share

That's because shot is only going to exist for one frame, and only inside the { } of the if.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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();
       }
  }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hardmode2236 · Apr 29, 2013 at 06:36 AM 0
Share

ohhh man it was a scope issue i see, thanks. can't believe i didn't notice.

avatar image whydoidoit · Apr 29, 2013 at 06:41 AM 0
Share

No problem - happens to us all :D Can you tick the answer for me?

avatar image hardmode2236 · Apr 29, 2013 at 06:44 AM 0
Share

yeah, thanks a million my gun shoots perfect now

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Rapid fire Help 1 Answer

Setting Scroll View Width GUILayout 1 Answer

help with bullet shooting code? - javascript 1 Answer

Shooting AI problem 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges