x


Message Displayer

Hi,

I have question about this:

http://www.unifycommunity.com/wiki/index.php?title=MessageDisplayer

using UnityEngine;
using System.Collections;

// Use this script on a guiText object to have status messages
// Just call messageDisplayerObject.DisplayMessage("hello") and you'll
// get a line of self disappearing messages.

public class MessageDisplayer : MonoBehaviour
{
    ArrayList messages = new ArrayList();

    public void DisplayMessage(string message)
    {
        messages.Add(message);
        UpdateDisplay();
        Invoke("DeleteOldestMessage", 5F);
    }

    void DeleteOldestMessage()
    {
        // The following "if statement" protects this function from
        // getting called by SendMessage from another script and
        // crashing.
        if (messages.Count > 0)
        {
            messages.RemoveAt(0);
            UpdateDisplay();
        }
    }

    void UpdateDisplay()
    {
        string formattedMessages = "";

        foreach (string message in messages)
        {
            formattedMessages += message + "\n";
        }

        guiText.text = formattedMessages;
    }
}

If I would like to use messageDisplayerObject.DisplayMessage("hello"), do I have to create an object of MessageDisplayer? If so how can I do that?

Thank you very much.

more ▼

asked Dec 30 '09 at 04:18 AM

niter412 gravatar image

niter412
123 6 6 15

Or how can I make this static so I do not have to create an object every time when I want to use the DispalyMessage function? I added the static before the void but it is saying that UpdateDispaly() also needs to be static so I changed that to static as well and now it's saying: An object reference is required for the nonstatic field, method or property.

Thank you!

Dec 30 '09 at 04:49 AM niter412
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You need to attach this script to a GUIText object. Then, you can make a call to nameOfYourGUITextObject.DisplayMessage in any script which has a reference to your GUIText object in order to display a message.

more ▼

answered Dec 30 '09 at 05:45 AM

Mortim gravatar image

Mortim
709 3 7 18

What you mean by reference to my GUIText object?

Dec 31 '09 at 03:04 AM niter412

A variable declared in some script which has your GUITextObject as a value.

Dec 31 '09 at 04:25 PM Mortim
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5058
x1999

asked: Dec 30 '09 at 04:18 AM

Seen: 1744 times

Last Updated: Dec 30 '09 at 04:18 AM