Salut!
I have a line
Debug.Log(this.collider.gameObject.name + " hit " + collider.SendMessageUpwards("Name"));
And compiler returns error
error CS0019: Operator +' cannot be applied to operands of type
string’ and `void’
I change it to just
Debug.Log(collider.SendMessageUpwards("Name"));
Yet compiler says:
error CS1503: Argument #1' cannot convert
void’ expression to type `object’
I also tried
Debug.Log(Convert.ChangeType(collider.SendMessageUpwards("Name"), typeof(message)));
But the result is:
error CS0103: The name `Convert’ does not exist in the current context
All I want is just obtain a name from the other object and insert it into log.
How to do it?
Thanks!
The problem is that function collider.SendMessageUpwards returns void, not a string:
http://docs.unity3d.com/Documentation/ScriptReference/Component.SendMessageUpwards.html
Just check the OnCollisionEnter function. If you just want to know the name of the object you are colliding with, use this:
void OnCollisionEnter(Collision other) {
Debug.Log(this.gameObject.name + " hit " + other.gameObject.name);
}
I think I solved the situation with your help.
Thank you all, folks!
-
Despite airplane has a rigidbody and meshcollider, the explosion did not collided with it.
By documentation, one rigid body should be sufficient for the collision to trigger.
I added a rigidbody to the explosion as well, and collision started to happen.
-
OnCollisionXXX happens only when some or both colliders are NOT triggers (haven’t understood it yet).
But this causes a strange behaviour, since shell bump off airplane region (not the airplane itself!).
The shell has 10x10x10cm cube collider.
No problem with the source. Though at this stage my answer is off-topic
This script is attached to the projectile shot from the cannon:
public class CannonAA_Shell : MonoBehaviour
{
public float LifeSeconds = 5f;
public GameObject AerialExplosion;
void Start()
{
Destroy(this.gameObject, LifeSeconds);
}
void OnDestroy()
{
Instantiate(AerialExplosion, transform.position, transform.rotation);
}
}
For the kind of heavy anti-aircraft cannons the direct his is almost impossible, and the main idea is to fill the volume with fragments of the exploding shell.
Therefore I don’t consider direct hit in this situation at all.
When the shell explodes, a new object is created, an explosion visualisation with a spheric collider, 100m radius.
public class CannonAA_Explo_Air : MonoBehaviour
{
void Start()
{
Destroy(this.gameObject, 10);
};
void OnTriggerStay(Collider collider)
{
if ( collider.gameObject.name.StartsWith("CannonAA_Shell") )
{
return;
}
Debug.Log(this.collider.gameObject.name + " hit " + collider.transform.parent.gameObject.name);
}
void OnCollisionStay(Collision collision)
{
if ( collision.gameObject.name.StartsWith("CannonAA_Shell") )
{
return;
}
Debug.Log("WOW! " + collision.gameObject.name);
}
}
When explosion’s collider “is trigger”, OnTriggerXXX() should be called.
When explosion’s collider “is NOT trigger”, OnCollisionXXX() should be called, but it’s not.