- Home /
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?
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).
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!
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
Follow this Question
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