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 andyfilms · Nov 12, 2013 at 03:34 AM · componentsendmessagenamespacebroadcastmessage

BroadcastMessage frustratng problem

Hi guys, I've spent the last four days on this and it has consumed me every waking second.

I'm working in FPS KIT 2. All I'm looking to do is to give a player a gun once he reaches a certain amount of kills. I'm able to pull the kills from the server, play a sound, etc. But trying to actually give him the gun is quite the challenge!

I've tried sendmessage, broadcastmessage, getcomponent, getcomponentsinchildren. Here's the gist of it;

 UpgradeLine.CS
 public AudioClip UpgradeSound;
     bool upgraded;
     int prevKills = 0;
     GameObject player = GameObject.FindGameObjectWithTag("Player");//Find local player
     string[] upgradeTemplate = new string[10];
     public GameObject upgrade3;
 
 
    void Update () {
         upgraded = (bool)PhotonNetwork.player.customProperties["Upgraded"];//Get weather we have been upgraded from the server
         upgradeLine((int)PhotonNetwork.player.customProperties["Kills"]);//I have no idea what i'm doing so I"m pulling kills from the server and hoping
     }
 
      void upgradeLine(int kills){
             if(upgraded == false){ //Don't do anything unless we haven't been upgraded yet
                 if(kills == 3 && kills != prevKills){ //Kill three
                             BroadcastMessage("addUpgradedGun", upgrade3);
                             upgraded = true;//Okay, we've been upgraded
                             Hashtable setPlayerUpgraded = new Hashtable() {{"Upgraded", upgraded}};//Tell server we've been upgraded
                             PhotonNetwork.player.SetCustomProperties(setPlayerUpgraded);
                             prevKills = kills;
                             audio.clip = UpgradeSound;//play sound
                             audio.Play();
                     }
                 } else {
                     upgraded = false;
                 }

And then here's the receiver, which is located on the same gameobject, not a child:

 WeaponPickUp.js
 public function AddUpgradedGun(gunAdd : WeaponScript){
             weapManager.allWeapons.Add(gunAdd);
             weapManager.SwitchWeapons(weapManager.SelectedWeapon.gameObject, weapManager.allWeapons[weapManager.allWeapons.Count-1].gameObject);
             weapManager.index = weapManager.allWeapons.Count-1;
             //Register Action
              actionsList.Add(("Picked | " + gunAdd.weaponName).ToString());
 }

I've tried placing the JS files in a folder called Standard Assets, I've also tried it the other way around with the CS files. Here's the error with this exact code:

 BroadcastMessage addUpgradedGun has no receiver!
 UnityEngine.Component:BroadcastMessage(String, Object)
 UpgradeLine:upgradeLine(Int32) (at Assets/FPS Kit Version 2.0/_CustomAssets/Scripts/Network C#/UpgradeLine.cs:49)
 UpgradeLine:Update() (at Assets/FPS Kit Version 2.0/_CustomAssets/Scripts/Network C#/UpgradeLine.cs:39)

Using the same thing with getComponent and declaring private WeaponPickUp weapPickUp, It won't even let me compile, stating:

 error CS0246: The type or namespace name `WeaponPickUp' could not be found. Are you missing a using directive or an assembly reference?
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Nov 12, 2013 at 03:55 AM

"addUpgradedGun" isn't the same as AddUpgradedGun - Unity is case sensitive, thus this single lowercase a screws up the whole thing. About GetComponent: scripts written in different languages can't see each other during compilation. For UpgradeLine.cs to find WeaponPickUp.js, the JS script must have been compiled in a previous "wave": move it to Standard Assets and the CS script to a custom folder (like Assets/SecondWave, for instance).

Comment
Add comment · Show 2 · 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 andyfilms · Nov 12, 2013 at 04:10 AM 0
Share

Corrected for case, got this doosy of an error:

 $$anonymous$$issing$$anonymous$$ethodException: The best match for method AddUpgradedGun has some invalid parameter.
 System.$$anonymous$$onoType.Invoke$$anonymous$$ember (System.String name, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args, System.Reflection.Parameter$$anonymous$$odifier[] modifiers, System.Globalization.CultureInfo culture, System.String[] namedParameters) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System/$$anonymous$$onoType.cs:520)
 UnityEngine.SetupCoroutine.Invoke$$anonymous$$ember (System.Object behaviour, System.String name, System.Object variable) (at C:/BuildAgent/work/ea95e74f6e5f192d/Runtime/Export/Coroutines.cs:19)
 UnityEngine.Component:Broadcast$$anonymous$$essage(String, Object)
 UpgradeLine:upgradeLine(Int32) (at Assets/FPS $$anonymous$$it Version 2.0/_CustomAssets/Scripts/Network C#/UpgradeLine.cs:51)
 UpgradeLine:Update() (at Assets/FPS $$anonymous$$it Version 2.0/_CustomAssets/Scripts/Network C#/UpgradeLine.cs:41)

Just for the record, all of these errors, even from the original post, have been with the JS files in a "standard assets" folder and the CS in another folder. Thanks for the reply!

avatar image aldonaletto · Nov 12, 2013 at 10:04 AM 1
Share

AddUpgradedGun expects to receive a reference to a script WeaponScript, but you're passing upgrade3, which's a GameObject reference - this causes the $$anonymous$$issing$$anonymous$$ethodException message.

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

17 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 avatar image avatar image avatar image

Related Questions

SendMessage and BroadcastMessage not getting received outside the script. 0 Answers

BroadcastMessage Functionality Help 1 Answer

Is it possible to use SendMessage() on an uninstantiated prefab? 0 Answers

Broadcast Message Problems 1 Answer

SendMessage getting "stuck" / looping animation, not returning to Update 1 Answer


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