Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Problems with InputSystem in UnityTiny

Discussion in 'Project Tiny' started by andrey_litvin, Dec 16, 2021.

  1. andrey_litvin

    andrey_litvin

    Joined:
    Jul 1, 2021
    Posts:
    7
    Strange error in the script in project UnityTiny. Somewhere the link to the object disappears, and where I can not understand. Help identify the problem. Thank you in advance.

    My script:
    Code (csharp):
    1. using Unity.Entities;
    2. using Unity.Tiny.Input;
    3. using Unity.Mathematics;
    4. using Unity.Tiny.Rendering;
    5.  
    6. namespace GameSystem
    7. {
    8.     public class InputSysTest : SystemBase
    9.     {
    10.         ScreenToWorld s2w;
    11.        
    12.         protected override void OnUpdate()
    13.         {
    14.             var input = World.GetExistingSystem<InputSystem>();
    15.             if (input.IsMousePresent())
    16.             {
    17.                 if (input.GetMouseButtonDown(0))
    18.                 {
    19.                     BeginMuve(input.GetInputPosition());
    20.                 }
    21.                 else if (input.GetMouseButton(0))
    22.                 {
    23.                     Muve(input.GetInputPosition());
    24.                 }
    25.                 else if (input.GetMouseButtonUp(0))
    26.                 {
    27.                     EndMuve();
    28.                 }
    29.             }
    30.             else if (input.IsTouchSupported())
    31.             {
    32.                 if (input.TouchCount() == 1)
    33.                 {
    34.                     Touch t0 = input.GetTouch(0);
    35.                     switch (t0.phase)
    36.                     {
    37.                         case TouchState.Began:
    38.                             BeginMuve(new float2(t0.x, t0.y));
    39.                             break;
    40.                         case TouchState.Moved:
    41.                             Muve(new float2(t0.x, t0.y));
    42.                             break;
    43.                         case TouchState.Canceled:
    44.                         case TouchState.Ended:
    45.                             EndMuve();
    46.                             break;
    47.                     }
    48.                 }
    49.             }      
    50.         }
    51.  
    52.         void BeginMuve(float2 inputPos)
    53.         {
    54.             float3 posStart = s2w.InputPosToWorldSpacePos(inputPos, 4.0f);          
    55.         }
    56.  
    57.         void Muve(float2 inputPos)
    58.         {
    59.             float3 pos = s2w.InputPosToWorldSpacePos(inputPos, 4.0f);
    60.         }
    61.  
    62.         void EndMuve()
    63.         {
    64.                     }    
    65.     }
    66. }

    Such an error occurs:

    Code (csharp):
    1. : System.NullReferenceException:.
    2.    � GameSystem.InputSysTest.BeginMuve(float2 inputPos)
    3.    � GameSystem.InputSysTest.OnUpdate()
    4.    � Unity.Entities.SystemBase.Update()
    5.    � Unity.Entities.ComponentSystemGroup.UpdateAllSystems()
    6.    � Unity.Entities.ComponentSystem.Update()
    7.    � Unity.Entities.World.Update()
    8.    � Unity.Tiny.UnityInstance.Update(Double timestampInSeconds)
    9.    � Unity.Tiny.EntryPoint.Program.<>c__DisplayClass0_0.<Main>b__0(Double timestampInSeconds)
    10.    � Unity.Platforms.RunLoopImpl.EnterMainLoop(RunLoopDelegate runLoopDelegate)
    11.    � Unity.Tiny.EntryPoint.Program.Main()
     
  2. Goularou

    Goularou

    Joined:
    Oct 19, 2018
    Posts:
    54
    your variable of type ScreenToWorld named s2w is NOT instantiated, thus the null exception error
    OR/AND the input system MAY not be instantiated either

    test both!

    i.e. if (s2w == null) Debug.Log("s2w is null!"); //forgot the specific use of Debug in Tiny, which is slightly different if I remember correctly

    do the same for your variable named input...
     
    Last edited: Dec 16, 2021
    andrey_litvin likes this.
  3. andrey_litvin

    andrey_litvin

    Joined:
    Jul 1, 2021
    Posts:
    7
    Thanks it helped.
    But a new problem arose. When I build for Windows, everything is fine. When I build for Web, an error occurred at the moment when the mouse cursor falls into the browser area where the application is running.

    Error in browser console:
    Code (csharp):
    1.  
    2. Uncaught RuntimeError: unreachable
    3.     at il2cpp::os::CrashHelpers::CrashImpl() (015bcaee:0x2ba0c9)
    4.     at il2cpp::os::CrashHelpers::Crash() (015bcaee:0x2b9f5e)
    5.     at tiny::vm::Exception::RaiseInvalidCastException(Il2CppObject*, TinyType*) (015bcaee:0x2b84b7)
    6.     at Object_GetHashCode_mFE7E8ED24F9C03EDB5AC4CF33375E57B7B34B751 (015bcaee:0x142509)
    7.     at ClownFishSystem_OnUpdate_m631FDF6ADF74DA70FA7F91C5FE439C2EA05DC3D0 (015bcaee:0xfca8)
    8.     at SystemBase_Update_m9ABDC8490E61C7C7A010F259C0CE94DB03F76270 (015bcaee:0x17c106)
    9.     at ComponentSystemGroup_UpdateAllSystems_m31AE52B0FBC764D0714A09D8EDFDD5C9D52B21C8 (015bcaee:0x15d6a6)
    10.     at ComponentSystemGroup_OnUpdate_mFB899980685B4F6B088098C1F766F773C0DC9B85 (015bcaee:0x15d2d0)
    11.     at ComponentSystem_Update_m28896FBD179C0241C8B015E4179DCAE23BFDA2FF (015bcaee:0x15ae0f)
    12.     at World_Update_m830072D163FEBBDED3A1449E6B234895ED517ACE (015bcaee:0x1819de)
    13.  
    Script:
    Code (csharp):
    1. using Unity.Entities;
    2. using Unity.Tiny.Input;
    3. using Unity.Mathematics;
    4. using Unity.Tiny.Rendering;
    5.  
    6. namespace GameSystem
    7. {
    8.     public class InputSysTest : SystemBase
    9.     {
    10.         ScreenToWorld s2w;
    11.         InputSystem input;
    12.        
    13.         protected override void OnUpdate()
    14.         {
    15.            if (s2w == null)
    16.            {
    17.                s2w = World.GetExistingSystem<ScreenToWorld>();
    18.            }
    19.            if (input == null)
    20.            {
    21.                input = World.GetExistingSystem<InputSystem>();
    22.             }          
    23.             if (input.IsMousePresent())
    24.             {
    25.                 if (input.GetMouseButtonDown(0))
    26.                 {
    27.                     BeginMuve(input.GetInputPosition());
    28.                 }
    29.                 else if (input.GetMouseButton(0))
    30.                 {
    31.                     Muve(input.GetInputPosition());
    32.                 }
    33.                 else if (input.GetMouseButtonUp(0))
    34.                 {
    35.                     EndMuve();
    36.                 }
    37.             }
    38.             else if (input.IsTouchSupported())
    39.             {
    40.                 if (input.TouchCount() == 1)
    41.                 {
    42.                     Touch t0 = input.GetTouch(0);
    43.                     switch (t0.phase)
    44.                     {
    45.                         case TouchState.Began:
    46.                             BeginMuve(new float2(t0.x, t0.y));
    47.                             break;
    48.                         case TouchState.Moved:
    49.                             Muve(new float2(t0.x, t0.y));
    50.                             break;
    51.                         case TouchState.Canceled:
    52.                         case TouchState.Ended:
    53.                             EndMuve();
    54.                             break;
    55.                     }
    56.                 }
    57.             }    
    58.         }
    59.  
    60.         void BeginMuve(float2 inputPos)
    61.         {
    62.             float3 posStart = s2w.InputPosToWorldSpacePos(inputPos, 4.0f);        
    63.         }
    64.  
    65.         void Muve(float2 inputPos)
    66.         {
    67.             float3 pos = s2w.InputPosToWorldSpacePos(inputPos, 4.0f);
    68.         }
    69.  
    70.         void EndMuve()
    71.         {               }  
    72.     }
    73. }
    74.  
     
  4. andrey_litvin

    andrey_litvin

    Joined:
    Jul 1, 2021
    Posts:
    7
    Found a solution to the problem
     
  5. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    You should get a reference to other systems upon creation using GetOrCreateSystem<>:
    Code (CSharp):
    1.     protected override void OnCreate() {
    2.       base.OnCreate();
    3.       inputSystem = World.GetOrCreateSystem<InputSystem>();
    4.       s2w = World.GetOrCreateSystem<ScreenToWorld>()
    5.     }
     
    andrey_litvin likes this.
  6. andrey_litvin

    andrey_litvin

    Joined:
    Jul 1, 2021
    Posts:
    7
    Thanks!