Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Geo.Ego · Oct 17, 2012 at 04:22 PM · javascriptgameobjectdontdestroyonloadsendmessage

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Oct 17, 2012 at 04:23 PM 0
Share

PrintText needs to be on the script "GlobalInput.js" is it?

avatar image whydoidoit · Oct 17, 2012 at 04:24 PM 0
Share

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.

avatar image Geo.Ego · Oct 17, 2012 at 05:08 PM 0
Share

@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.

avatar image whydoidoit · Oct 17, 2012 at 05:11 PM 2
Share

See the article I linked - it's because you are using the " " version of GetComponent.

avatar image Seth-Bergman · Oct 17, 2012 at 05:32 PM 0
Share

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..

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges