Question by
replay55 · Oct 26, 2015 at 06:55 PM ·
sendmessageobject-reference-error
SendMessage/Object Reference error
So, I have two bits of code. One calls to another script to do something, and that script... Does something.
My error is that this:
void OnTriggerEnter(Collider Col){
if (inventory.charge == 4) {
transform.FindChild ("door").SendMessage ("DoorCheck");
if (GameObject.Find ("chargeHUDGUI")) {
Destroy (GameObject.Find ("chargeHUDGUI"));
doorlight.color = Color.green;
}
} else if(inventory.charge > 0 && inventory.charge < 4){
TextHints.SendMessage("I think it needs more power!" , "ShowHint", UnityEngine.SendMessageOptions);
} else {
AudioSource.PlayClipAtPoint (lockedsound, transform.position);
Col.gameObject.SendMessage("HUDon");
TextHints.SendMessage("This Door is locked, I need to power it!" , "ShowHint", UnityEngine.SendMessageOptions);
}
On TextHints.SendMessage, needs the string (The first line of text in the SendMessage) and the object, however, I'm not aware of what I need for those. Here's the script it's calling to:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextHints : MonoBehaviour {
Text text;
public float timer = 0.0f;
// Use this for initialization
void Start () {
text = GetComponent<Text>();
}
// Update is called once per frame
void Update () {
if (text.enabled) {
timer += Time.deltaTime;
}
if (timer >= 4) {
text.enabled = false;
timer = 0.0f;
}
}
void ShowHint(string message){
text.text = message;
if(!text.enabled){
text.enabled = true;
}
}
}
Comment
Best Answer
Answer by Jessespike · Oct 26, 2015 at 08:02 PM
Seems you have the parameters mixed up. Try swapping the methodname and the value, like this:
TextHints.SendMessage("ShowHint", "This Door is locked, I need to power it!", UnityEngine.SendMessageOptions);
http://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html
public void SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);
methodName: The name of the method to call.
value: An optional parameter. value to pass to the called method.
options: Should an error be raised if the method doesn't exist on the target object?