- Home /
.sendmessage not working C#
I don't desperately need send message as I just need to instantiate a prefab with a script, and tell the that scrip "this is the number you are going to display." either way this is what I have.
GameObject display = (GameObject)Instantiate (dmgdisplay, transform.position, transform.rotation)as GameObject;
display.transform.parent = GameObject.Find("slimy").transform;
display.SendMessage("display",dmg,SendMessageOptions.DontRequireReceiver);
Debug.Log ("Damage: " + dmg);
my other script is as follows
using UnityEngine;
using System.Collections;
public class dmgdisplay : MonoBehaviour {
void display(float dmg){
Destroy(gameObject, 2f);
GetComponent<TextMesh>().text = "" + dmg;
}
}
The prefab get created, but the function never gets called. If you can suggest an alternative method for passing information that would be equally as helpful.
Answer by musaranya · Jul 07, 2014 at 08:18 AM
I think that you can declare the function display as public and then, after instantiating an object that contains the dmgdisplay script, you can call this function as follows:
display.GetComponent().display(dmg);
I received this error:
error CS0309: The type UnityEngine.GameObject' must be convertible to
UnityEngine.Component' in order to use it as parameter T' in the generic type or method
UnityEngine.GameObject.GetComponent()'
Set the display method to be public, make sure that the script dmgdisplay is attached to your prefab, and try this code:
GameObject display = (GameObject)Instantiate (dmgdisplay, transform.position, transform.rotation)as GameObject;
display.transform.parent = GameObject.Find("slimy").transform;
display.GetComponent<dmgdisplay>().display(dmg);