- Home /
SendMessage on DontDestroyOnLoad object
I have a script on a GameObject that I'd like to interact with. I have a loading scene where I create some empty GameObjects to hold scripts for certain global features. I persist them with DontDestroyOnLoad. I would have thought that I could then do the following:
private var inputObject : GameObject;
function Start()
{
inputObject = GameObject.FindGameObjectWithTag ("GlobalInput");
}
function PrintText
{
inputObject.GetComponent("GlobalInput").SendMessage("PrintText");
}
However, when I do this, I get the error that SendMessage does not find a receiver. It would seem then that the GlobalInput object is being found, but the GlobalInput script attached to it is not able to receive the SendMessage. I'm wondering where I've gone wrong. Thanks.
PrintText needs to be on the script "GlobalInput.js" is it?
BTW you shouldn't really use the "xxx" version of GetComponent normally - it's slower and not much help when you try to access your own functions. In your case you shouldn't need to Send$$anonymous$$essage - just call the method. See here for details on how to use GetComponent.
@whydoidoit Yes, PrintText is a function on GlobalInput.js, which is attached to the GlobalInput GameObject. Also, I did try calling the method directly; I get the error that PrintText is not a member, but again, I know it does exist; I can call it from elsewhere in the script itself and it works fine.
See the article I linked - it's because you are using the " " version of GetComponent.
nice article, haven't seen that one before, thanks!
anyway, to re-word what @whydoidoit is saying, just get rid of the quotes:
inputObject.GetComponent(GlobalInput).Send$$anonymous$$essage("PrintText");
or just:
inputObject.GetComponent(GlobalInput).PrintText();
this way the returned component is of the type GlobalInput..
WITH the quotes, you actually get a return type of Object which would then need to be typecast..
Answer by Geo.Ego · Dec 21, 2012 at 06:48 PM
Since the correct answer was submitted as a comment, I'm adding it as an answer. Basically, removing the quotes from the parameter passed to GetComponent did the trick.
Your answer
Follow this Question
Related Questions
Java Script Dont Destroy On Load Duplicates Game Object 3 Answers
SendMessage with class. 0 Answers
Trying to make a simple inventory: 0 Answers
Need help with building system 1 Answer
Creating a teleportation gun 1 Answer