Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Referenceerror: MyGameInstance is not defined Unity 2021.2

Discussion in 'WebGL' started by oranchad, Mar 29, 2022.

  1. oranchad

    oranchad

    Joined:
    May 26, 2020
    Posts:
    5
    My WebGL build using Unity 2021.2
    I'm trying to get my jslib plugin to talk back to Unity but can't even get it to return to Unity no matter what I try.

    According to the documentation Unity - Manual: Interaction with browser scripting (unity3d.com)
    I've edited the build index.html to include the myGameObject (myUnityInstance)

    I've setup a button click to trigger the js code and then throws the error

    ReferenceError: MyUnityInstance is not defined as _GetString

    the js code

    Code (JavaScript):
    1. mergeInto(
    2.   LibraryManager.library,
    3.   {
    4.     GetString: function(){
    5.  
    6.       var str = "This is a string";
    7.       var buffersize = lengthBytesUTF8(str) +1;
    8.       var buffer = _malloc(buffersize);
    9.       stringToUTF8(str, buffer, buffersize);    
    10.       MyUnityInstance.SendMessage('JSUrl', 'JSReturnString', 'buffer');
    11.       return buffer;
    12.      
    13.     }
    14.   }
    15.    
    16. );
    the C# function

    Code (CSharp):
    1.     public void JSReturnString(string returnStr)
    2.     {
    3.          print("Returned String: " + returnStr);
    4.  
    5.     }
    index.html

    Code (Boo):
    1.       var myUnityInstance = null;
    2.       script.src = loaderUrl;
    3.       script.onload = () => {
    4.         createUnityInstance(canvas, config, (progress) => {
    5.           progressBarFull.style.width = 100 * progress + "%";
    6.         }).then((unityInstance) => {
    7.             myUnityInstance = unityInstance;
    8.           loadingBar.style.display = "none";
    9.           fullscreenButton.onclick = () => {
    10.             unityInstance.SetFullscreen(1);
    11.           };
    12.         }).catch((message) => {
    13.           alert(message);
    14.         });
    15.       };
     
    AfricanStudios likes this.
  2. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    584
    You create myUnityInstance but you use MyUnityInstance
     
  3. oranchad

    oranchad

    Joined:
    May 26, 2020
    Posts:
    5
    It throws the same error unfortunately. myUnityInstance is undefined
     
  4. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    584
    Can you try to replace myUnityInstance = unityInstance; by window.MyUnityInstance = unityInstance;

    You use "MyUnityInstance.SendMessage" in your JS, so it must be MyUnityInstance with an uppercase not myUnityInstance with a lowercase

    adding "window." will give you a global scope.
     
    oranchad likes this.
  5. oranchad

    oranchad

    Joined:
    May 26, 2020
    Posts:
    5
    So I managed to get it working in the end.

    I didn't need MyUnityInstance, instead just used
    Code (JavaScript):
    1.  
    2. SendMessage('JSUrl', 'JSReturnString', 'buffer');
    3.  
    I had tried that bit of code before but didn't work but now it does. It is passing "buffer" now.
    I believe when I tried before I was trying to get to print to a UI label and that was not setup right hence the confusion.

    It was the classic moment, my code doesn't work and don't know why. My code works and don't know why.

    That said I was not aware that window give me global scope so thank you for that.
     
    romachizh and AfricanStudios like this.
  6. AfricanStudios

    AfricanStudios

    Joined:
    Apr 14, 2020
    Posts:
    2
    I spent 3 days trying to get async to return. Thank so much for the clarification. Surprised that this is still an issue. This worked when my script was attach to gameObject.
    Code (CSharp):
    1. SendMessage('ObjectName', 'Function In Script(not ScriptName)', 'Message');