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 Steven-1 · Jan 17, 2012 at 03:05 PM · webplayersendmessagebrowserunityobject

unityObject is null

I'm trying to call a Unity function from the browser. (see http://unity3d.com/support/documentation/Manual/Unity%20Web%20Player%20and%20browser%20communication.html)

so I do this:

 var unity = unityObject.getObjectById("UnityContent");
 unity.SendMessage("Controller", "SetBottleColor", "2caffe");

but this doesn't work because unityObject is null.

I have no idea what I'm dooing wrong, I thought I might call this too soon, and the web player's content isn't loaded yet. But I'm now triggering this when I press a button on the page an it's still the same problem.

Any help?

Edit: here's the entire javascript part of the html file:

     <script type="text/javascript" src="http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject.js"></script>
     <script type="text/javascript">
     <!--
     if (typeof unityObject != "undefined") {
         unityObject.embedUnity("unityPlayer", "WebPlayer.unity3d", 600, 450, null, null, unityLoaded);
     }
     function unityLoaded(result) 
     {
         if (result.success) {
             var unity = result.ref;
             var version = unity.GetUnityVersion("3.x.x");
             alert("Unity Web Player loaded!\nId: " + result.id + "\nVersion: " + version);
             //SentbottleColor();
         }
         else 
         {
             alert("Please install Unity Web Player!");
         }
     }
     function SentbottleColor()
     {
         alert("SetBottleColor start");
         var unity = unityObject.getObjectById("UnityContent");
         unity.SendMessage("Controller", "SetBottleColor", "2caffe");
         alert("SetBottleColor end");
     }
     -->
     </script>
Comment
Add comment · Show 2
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 Kryptos · Jan 17, 2012 at 03:23 PM 0
Share

Did you insert this piece of code into the HT$$anonymous$$L file generated by Unity during WebPlayer build?

Did you make sure that the unityObject was created before your piece of code?

avatar image Steven-1 · Jan 17, 2012 at 03:31 PM 0
Share

No, I have a seperate html file (which is actually a copy of the generated file with extra stuff added). And yes, well actually, I thinks so, because unityObject.embedUnity() seems to work.

2 Replies

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

Answer by Shchvova · Jan 17, 2012 at 03:30 PM

Hey! There is couple things which can go wrong. First - ensure your Unity object have ID "UnityContent". Than, try to write your code in browser console (Ctrl+Shift+J in Firefox and Chrome) line by line. If it will work, than problem is that you calling your code while object is not created yet.

UPD: You can not access unity object just before it was created. You should wait while it is loaded and ready to accept messages. I'd recommend to determine it by first sending message to browser javascript. For example In Unity do something like this: Application.ExternalCall( "UnityIsReady"); in Start() function and in web page javascript write something like

 <script>
 function UnityIsReady()
 {
     var unity = unityObject.getObjectById("UnityContent");
     unity.SendMessage("MyObject", "MyFunction", "Hello from a web page!");
 }
 
 </script>

UPD2: I believe this is typo. Your IDs are different. Replace UnityContent with unityPlayer

Comment
Add comment · Show 6 · 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
avatar image Steven-1 · Jan 17, 2012 at 03:52 PM 0
Share

That's a good idea, but I only do the send$$anonymous$$essage when I press a button on the page, and loading should be long done by then

avatar image Shchvova · Jan 17, 2012 at 04:04 PM 0
Share

Lol... "First - ensure your Unity object have ID "UnityContent". It is not.

avatar image Steven-1 · Jan 17, 2012 at 04:13 PM 0
Share

oh, ok, I didn't got what you meant. I didn't know the first parameter that was sent is the id you have to call later. the unityObject.embedUnity() part was already there when generated by Unity, I thought that first parameter was just the name of the webplayer or something. And I then looked at http://unity3d.com/support/documentation/$$anonymous$$anual/Unity%20Web%20Player%20and%20browser%20communication.html and they use a different name there (with is quiet misleading).

Thanks for the help, damn, I've wasted way too much on this.

avatar image Kryptos · Jan 17, 2012 at 04:22 PM 0
Share

Yes the documentation is sometimes misleading.

This one though is consistent from start to end: http://unity3d.com/support/documentation/$$anonymous$$anual/Working%20with%20UnityObject.html

avatar image Shchvova · Jan 17, 2012 at 04:31 PM 0
Share

Welcome! (don't forget to +1 ^^ )

Show more comments
avatar image
0

Answer by Shchvova · Jan 17, 2012 at 09:12 PM

I bielieve your ids id different... Try var unity = unityObject.getObjectById("unityPlayer"); instead of var unity = unityObject.getObjectById("UnityContent");

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

7 People are following this question.

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

Related Questions

Full screen not working from html message 0 Answers

How would I go about getting the results of a javascript variable to a string inside script? 3 Answers

Do joysticks only work in the desktop version??? 3 Answers

Why cant some users access my game using Chrome? 1 Answer

How can you detect in javascript when UnityObject2 fails to load the unity3d file? 0 Answers


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