- Home /
broadcasting error for targetting script
I APOLOGIZE TRULY, after seeing the scripts all layed out on here like this i saw the problem very quickly lol, i labled "show mob vitals bars" without a space in the vital script, so the fix would be spacing this out in the vitalbars.cs script or by removing the spaces in this first script, again sorry for the wasted space lol
BroadcastException: Broadcasting message show mob vital bars but no listener found. MessengerInternal.OnBroadcasting (System.String eventType, MessengerMode mode) (at Assets/_Scripts/_Messenger/Messenger.cs:64) Messenger`1[System.Boolean].Broadcast (System.String eventType, Boolean arg1, MessengerMode mode) (at Assets/_Scripts/_Messenger/Messenger.cs:139) Messenger`1[System.Boolean].Broadcast (System.String eventType, Boolean arg1) (at Assets/_Scripts/_Messenger/Messenger.cs:135) Targeting.DeselectTarget () (at Assets/_Scripts/_Player/Targeting.cs:87) Targeting.TargetEnemy () (at Assets/_Scripts/_Player/Targeting.cs:63) Targeting.Update () (at Assets/_Scripts/_Player/Targeting.cs:93) ive searched endlessly and i cannot solve this problem, any help would be a god send lol
heres the code
/// this script canbe attached to any permanent gameobject. and is responsible for allowing the player to target different mobs that are within range
/// </summary>
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Targeting : MonoBehaviour {
public List<Transform> targets;
public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
public void AddTarget(Transform enemy)
{
targets.Add (enemy);
}
private void SortTargetsByDistance()
{
targets.Sort(delegate(Transform t1, Transform t2) {
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
}
private void TargetEnemy()
{
if(selectedTarget == null)
{
SortTargetsByDistance();
selectedTarget = targets[0];
}
else
{
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count - 1)
{
index++;
}
else
{
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
private void SelectTarget() {
Transform name = selectedTarget.FindChild("Name");
if(name == null) {
Debug.LogError("Could not find the name on " + selectedTarget.name);
return;
}
name.GetComponent<TextMesh>().text = selectedTarget.GetComponent<Mob>().Name;
name.GetComponent<MeshRenderer>().enabled = true;
selectedTarget.GetComponent<Mob>().DisplayHealth();
Messenger<bool>.Broadcast("show mob vital bars", true);
}
private void DeselectTarget() {
selectedTarget.FindChild("Name").GetComponent<MeshRenderer>().enabled = false;
selectedTarget = null;
Messenger<bool>.Broadcast("show mob vital bars", false);
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab)) {
TargetEnemy();
}
}
}
oh and here is messenger.cs sorry / Messenger.cs v1.0 by Magnus Wolffelt, magnus.wolffelt@gmail.com // // Inspired by and based on Rod Hyde's Messenger: // http://www.unifycommunity.com/wiki/index.php?title=CSharpMessenger // // This is a C# messenger (notification center). It uses delegates // and generics to provide type-checked messaging between event producers and // event consumers, without the need for producers or consumers to be aware of // each other. The major improvement from Hyde's implementation is that // there is more extensive error detection, preventing silent bugs. // // Usage example: // Messenger.AddListener("myEvent", MyEventHandler); // ... // Messenger.Broadcast("myEvent", 1.0f);
using System;
using System.Collections.Generic;
public enum MessengerMode {
DONT_REQUIRE_LISTENER,
REQUIRE_LISTENER,
}
static internal class MessengerInternal {
static public Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();
static public readonly MessengerMode DEFAULT_MODE = MessengerMode.REQUIRE_LISTENER;
static public void OnListenerAdding(string eventType, Delegate listenerBeingAdded) {
if (!eventTable.ContainsKey(eventType)) {
eventTable.Add(eventType, null);
}
Delegate d = eventTable[eventType];
if (d != null && d.GetType() != listenerBeingAdded.GetType()) {
throw new ListenerException(string.Format("Attempting to add listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being added has type {2}", eventType, d.GetType().Name, listenerBeingAdded.GetType().Name));
}
}
static public void OnListenerRemoving(string eventType, Delegate listenerBeingRemoved) {
if (eventTable.ContainsKey(eventType)) {
Delegate d = eventTable[eventType];
if (d == null) {
throw new ListenerException(string.Format("Attempting to remove listener with for event type {0} but current listener is null.", eventType));
} else if (d.GetType() != listenerBeingRemoved.GetType()) {
throw new ListenerException(string.Format("Attempting to remove listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being removed has type {2}", eventType, d.GetType().Name, listenerBeingRemoved.GetType().Name));
}
} else {
throw new ListenerException(string.Format("Attempting to remove listener for type {0} but Messenger doesn't know about this event type.", eventType));
}
}
static public void OnListenerRemoved(string eventType) {
if (eventTable[eventType] == null) {
eventTable.Remove(eventType);
}
}
static public void OnBroadcasting(string eventType, MessengerMode mode) {
if (mode == MessengerMode.REQUIRE_LISTENER && !eventTable.ContainsKey(eventType)) {
throw new MessengerInternal.BroadcastException(string.Format("Broadcasting message {0} but no listener found.", eventType));
}
}
static public BroadcastException CreateBroadcastSignatureException(string eventType) {
return new BroadcastException(string.Format("Broadcasting message {0} but listeners have a different signature than the broadcaster.", eventType));
}
public class BroadcastException : Exception {
public BroadcastException(string msg)
: base(msg) {
}
}
public class ListenerException : Exception {
public ListenerException(string msg)
: base(msg) {
}
}
}
// No parameters
static public class Messenger {
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable;
static public void AddListener(string eventType, Callback handler) {
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback)eventTable[eventType] + handler;
}
static public void RemoveListener(string eventType, Callback handler) {
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
}
static public void Broadcast(string eventType) {
Broadcast(eventType, MessengerInternal.DEFAULT_MODE);
}
static public void Broadcast(string eventType, MessengerMode mode) {
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d)) {
Callback callback = d as Callback;
if (callback != null) {
callback();
} else {
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
}
// One parameter
static public class Messenger<T> {
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable;
static public void AddListener(string eventType, Callback<T> handler) {
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T>)eventTable[eventType] + handler;
}
static public void RemoveListener(string eventType, Callback<T> handler) {
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
}
static public void Broadcast(string eventType, T arg1) {
Broadcast(eventType, arg1, MessengerInternal.DEFAULT_MODE);
}
static public void Broadcast(string eventType, T arg1, MessengerMode mode) {
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d)) {
Callback<T> callback = d as Callback<T>;
if (callback != null) {
callback(arg1);
} else {
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
}
// Two parameters
static public class Messenger<T, U> {
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable;
static public void AddListener(string eventType, Callback<T, U> handler) {
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T, U>)eventTable[eventType] + handler;
}
static public void RemoveListener(string eventType, Callback<T, U> handler) {
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T, U>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
}
static public void Broadcast(string eventType, T arg1, U arg2) {
Broadcast(eventType, arg1, arg2, MessengerInternal.DEFAULT_MODE);
}
static public void Broadcast(string eventType, T arg1, U arg2, MessengerMode mode) {
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d)) {
Callback<T, U> callback = d as Callback<T, U>;
if (callback != null) {
callback(arg1, arg2);
} else {
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
}
// Three parameters
static public class Messenger<T, U, V> {
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable;
static public void AddListener(string eventType, Callback<T, U, V> handler) {
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T, U, V>)eventTable[eventType] + handler;
}
static public void RemoveListener(string eventType, Callback<T, U, V> handler) {
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T, U, V>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
}
static public void Broadcast(string eventType, T arg1, U arg2, V arg3) {
Broadcast(eventType, arg1, arg2, arg3, MessengerInternal.DEFAULT_MODE);
}
static public void Broadcast(string eventType, T arg1, U arg2, V arg3, MessengerMode mode) {
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d)) {
Callback<T, U, V> callback = d as Callback<T, U, V>;
if (callback != null) {
callback(arg1, arg2, arg3);
} else {
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
}
one more edit sorry, if im not being proper to the forumns, i believe the error could derive from this vital bars script, even though the compiler doesnt mention it
/// VitalBar.cs
/// 4 14 2012
/// joey
/// this class is responsible for displaying a vital for the player character or a mob
/// </summary>
using UnityEngine;
using System.Collections;
public class VitalBar : MonoBehaviour {
public bool _isPlayerHealthBar; //this bool value tells us if this the player health or mob healthbar
private int _maxBarLength; //how long the vital bar can be at its maximum health
private int _curBarLength; // this is the current length of the vital bar
private GUITexture _display;
// Use this for initialization
void Start () {
// _isPlayerHealthBar = true;
_display = gameObject.GetComponent<GUITexture>();
_maxBarLength = (int)_display.pixelInset.width;
OnEnable();
}
// Update is called once per frame
void Update () {
}
//this method is called when the gameObject is enabled
public void OnEnable() {
if(_isPlayerHealthBar)
Messenger<int, int>.AddListener("player health update", OnChangeHealthBarSize);
else {
ToggleDisplay(false);
Messenger<int, int>.AddListener("mob health update", OnChangeHealthBarSize);
Messenger<bool>.AddListener("show mob vitalbars", ToggleDisplay);
}
}
//this method is called when the gameobject is disabled
public void OnDisable() {
if(_isPlayerHealthBar)
Messenger<int, int>.RemoveListener("player health update", OnChangeHealthBarSize);
else {
Messenger<int, int>.RemoveListener("mob health update", OnChangeHealthBarSize);
Messenger<bool>.RemoveListener("show mob vitalbars", ToggleDisplay);
}
}
// this method will calculate the total size of the health bar in relation to the x of health the target has left
public void OnChangeHealthBarSize(int curHealth, int maxHealth) {
// Debug.Log("We heard an event: curHealth - " + curHealth + " - maxhealth - " + maxhealth);
_curBarLength = (int)((curHealth / (float)maxHealth) * _maxBarLength); //this calculates the current bar length based on the health %
//_display.pixelInset = new Rect(_display.pixelInset.x, _display.pixelInset.y, _curBarlength, _display.pixelInset.height);
_display.pixelInset = CalculatePosition();
}
//setting the healthbar to the player or mob
public void SetPlayerHealth(bool b) {
_isPlayerHealthBar = b;
}
private Rect CalculatePosition() {
float yPos = _display.pixelInset.y / 2 - 15;
if(!_isPlayerHealthBar) {
float xPos = (_maxBarLength - _curBarLength) - (_maxBarLength / 4 + 38);
return new Rect(xPos, yPos, _curBarLength, _display.pixelInset.height);
}
return new Rect(_display.pixelInset.x, _display.pixelInset.y, _curBarLength, _display.pixelInset.height);
}
private void ToggleDisplay(bool show) {
_display.enabled = show;
}
}
at the top it says no listener found seems you need to install one and you should be set.if i recall that is a camera can't be 100% on that as its been a long time since i saw that error.
thank you, i fixed this error and posted what was causing it, but maybe that is the root of my problem, cause i have a new error lol thanks again
Answer by CarstenHouweling · Apr 14, 2012 at 06:25 PM
Since you use all the code from the hack and slash tutorials from BurgZergArcade, why don't you ask him?
well i didnt want to bother him with my little questions... thanks for your crappy attitude =]
and also i already solved this issue so your comment was for nothing lol
Answer by Grayie · Jul 26, 2012 at 09:38 AM
Hey, Joey. Have you fixed your "new" problem? I've got the same. Don't know what to do. All codes are the same.
So, I got "show mob vital bars" everywhere, and nothing. Error: "broadcasting message show mob vital bars but no listener found", when i press "tab". It just targeting my first mob, then target "none", then again it t. first mob, "none", etc... There are also 2 "NullReferenceException" errors in VitalBars.cs (lines with "ToggleDisplay" function) and 1 in Game$$anonymous$$aster.cs 8[
hope you fix this, but it sounds like some of your errors might be cause somethings not attached to something or what not, im not such a great scripter so im sorry i cant help you much lol
Answer by joeyaaaaa · Jul 27, 2012 at 09:19 AM
The new error was fixed and irrelevant I believe, I don't remember but the fix is written at the top