Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges