I’m getting much of the same error.
Specifically, I have a DamageDealer script attached to something with collision.
It calls SendMessageUpwards() on the gameObject of whatever it collides with; this message ends up in a DamageTaker script.
The DamageTaker script then calls BroadcastMessage(“Die”, null) on its own gameObject.
Inside that BroadcastMessage callback, I get this error/assert message.
I don’t get this message in build-and-run mode (that I can see, but maybe it’s just swallowed), only in run-inside-editor mode. It’s stopping me from making progress, because I always keep “pause on error” on so as to take care of any problems ASAP.
I’m using 2.6.1f3 (31223)
Here’s the DamageTaker script, which is where the error happens (the call to BroadcastMessage in TakeDamage):
using UnityEngine;
using System.Collections;
public class DamageTaker : MonoBehaviour {
public float hitPoints = 1.0f;
private float actualHitPoints;
public void Start() {
Debug.Log("DamageTaker.Start() " + gameObject.name);
actualHitPoints = hitPoints;
}
public void TakeDamage(float amt) {
actualHitPoints -= amt;
Debug.Log("DamageTaker.TakeDamage() " + gameObject.name);
if (actualHitPoints <= 0) {
if (gameObject.active) {
gameObject.BroadcastMessage("Die", null);
}
// todo: create particle system, sound, etc
}
}
public void Die() {
if (gameObject.active) {
gameObject.active = false;
enabled = false;
Destroy(gameObject);
}
}
}
Here’s the DamageDealer script, which calls SendMessageUpwards() on the mesh object that is inside the prefab object that was spawned:
using UnityEngine;
using System.Collections;
public class DamageDealer : MonoBehaviour {
public float damageAmount = 1.0f;
void OnCollisionEnter(Collision c) {
Debug.Log("DamageDealer.OnCollisionEnter() " + c.gameObject.name);
c.gameObject.SendMessageUpwards("TakeDamage", damageAmount);
gameObject.BroadcastMessage("Die", null, SendMessageOptions.DontRequireReceiver);
}
public void Die() {
if (gameObject.active) {
gameObject.active = false;
enabled = false;
Destroy(gameObject);
}
}
}
Here’s how I spawn the thing getting hit, specifically look at SpawnAtTargeting():
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnStringScript : MonoBehaviour {
public GameObject toSpawn = null;
public int numSpawned = 8;
public float spacing = 5.0f;
public int numTimesSpawning = 1;
public float delayBetweenSpawns = 30;
public float timeUntilFirstSpawn = 1;
float timeUntilSpawn;
int timesToSpawn;
Vector3 GetNavpoint() {
foreach (Transform t in gameObject.transform) {
return t.position;
}
return new Vector3(0, 0, 0);
}
// Use this for initialization
void Start () {
timeUntilSpawn = timeUntilFirstSpawn;
timesToSpawn = numTimesSpawning;
}
void Spawn() {
SpawnAtTargeting(gameObject.transform.position, GetNavpoint());
}
void SpawnAt(Vector3 pos) {
SpawnAtTargeting(pos, GetNavpoint());
}
void SpawnAtTargeting(Vector3 pos, Vector3 tgt) {
Debug.Log(String.Format("SpawnAtTargeting({0}, {1}, {2})", pos, tgt, toSpawn));
Vector3 d = (pos - tgt);
d.Normalize();
GameObject go = new GameObject();
go.active = true;
StringController sc = go.AddComponent("StringController") as StringController;
sc.spacing = spacing;
sc.pathParent = gameObject.transform;
Quaternion q = new Quaternion();
q.SetFromToRotation(new Vector3(0, 0, 1), d);
for (int i = 0; i < numSpawned; ++i) {
GameObject obj = (GameObject)GameObject.Instantiate(toSpawn, pos, q);
sc.Add(obj);
pos = pos + (d * spacing);
}
}
// Update is called once per frame
void Update () {
if (timesToSpawn > 0) {
timeUntilSpawn -= Time.deltaTime;
if (timeUntilSpawn <= 0) {
timeUntilSpawn += delayBetweenSpawns;
timesToSpawn -= 1;
Spawn();
}
}
}
}
Here’s how I spawn the thing doing the colliding, specifically look at Update() (ought to be factored, I know :-):
using UnityEngine;
using System.Collections;
public class Player2DScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
public float posX = -10;
public float posZ = -10;
public float hMoveSpeed = 10;
public float hiLimitX = 20;
public float loLimitX = -20;
public Transform laserPrefab = null;
public float shotInterval = 0.5f;
public float laserVelocity = 20.0f;
private float timeToNextShot = 0.5f;
// Update is called once per frame
void Update () {
float hMove = Input.GetAxis("Horizontal");
posX += hMove * hMoveSpeed * Time.deltaTime;
if (posX > hiLimitX)
posX = hiLimitX;
if (posX < loLimitX)
posX = loLimitX;
transform.position = new Vector3(posX, 0, posZ);
if (timeToNextShot > 0) {
timeToNextShot -= Time.deltaTime;
}
if ((timeToNextShot <= 0) Input.GetButton("Fire1")) {
Debug.Log("Fire triggered");
timeToNextShot = shotInterval;
GameObject bullet = (GameObject)GameObject.Instantiate(laserPrefab,
new Vector3(transform.position.x, transform.position.y, transform.position.z + 4),
Quaternion.AngleAxis(90, Vector3.right));
bullet.GetComponent<Rigidbody>().velocity = new Vector3(0, 0, laserVelocity);
}
}
}
Finally, here is the full error/stack message:
!IsActive () !m_RunInEditMode
UnityEngine.GameObject:BroadcastMessage(String, Object, SendMessageOptions)
UnityEngine.GameObject:BroadcastMessage(String, Object, SendMessageOptions)
UnityEngine.GameObject:BroadcastMessage(String, Object)
DamageTaker:TakeDamage(Single) (at Assets\Scripts\DamageTaker.cs:19)
UnityEngine.GameObject:SendMessageUpwards(String, Object, SendMessageOptions)
UnityEngine.GameObject:SendMessageUpwards(String, Object, SendMessageOptions)
UnityEngine.GameObject:SendMessageUpwards(String, Object)
DamageDealer:OnCollisionEnter(Collision) (at Assets\Scripts\DamageDealer.cs:10)
[..\..\Runtime\Mono\MonoBehaviour.cpp line 808]