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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

invalidOperationException: The NativeContainer waitJob.result has not been assigned or constructed.

Discussion in 'Scripting' started by hydos06, Nov 8, 2019.

  1. hydos06

    hydos06

    Joined:
    Oct 18, 2019
    Posts:
    10
    hello, i am trying to make a script wait for a bit and i found out about jobs. i want to get the job to wait then the script to wait for the job to finish. if i waited in the script the whole engine waits. this is my current code and error

    InvalidOperationException: The NativeContainer waitJob.result has not been assigned or constructed. All containers must be valid when scheduling a job.
    Unity.Jobs.LowLevel.Unsafe.JobsUtility.Schedule (Unity.Jobs.LowLevel.Unsafe.JobsUtility+JobScheduleParameters& parameters) (at <5e35e4589c1948aa8af5b8e64eea8798>:0)
    Unity.Jobs.IJobExtensions.Schedule[T] (T jobData, Unity.Jobs.JobHandle dependsOn) (at <5e35e4589c1948aa8af5b8e64eea8798>:0)
    CodeParser.checkMethods (Variable[] vArgs, System.String method) (at Assets/scripts/Project/CodeParser.cs:121)
    CodeParser.parse (System.String line) (at Assets/scripts/Project/CodeParser.cs:101)


    codeParser.cs
    Code (CSharp):
    1.  NativeArray<float> result = new NativeArray<float>(1, Allocator.TempJob);
    2.  
    3.  
    4. if(method == "wait")
    5.         {
    6.             /*
    7.              * Wait for a certian ammount of milliseconds
    8.              */
    9.             int time = (int)vArgs[0].getVariable();
    10.             waitJob jobData = new waitJob();
    11.             jobData.time = time;
    12.  
    13.             // Schedule the job
    14.             JobHandle handle = jobData.Schedule();
    15.  
    16.             // Wait for the job to complete
    17.             handle.Complete();
    18.         }
    waitjob.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Threading;
    5. using Unity.Jobs;
    6. using Unity.Collections;
    7.  
    8.  
    9. // Job waiting a certian ammount of time
    10. public struct waitJob : IJob
    11. {
    12.     public int time;
    13.  
    14.     public NativeArray<float> result;
    15.  
    16.     public void Execute()
    17.     {
    18.         Debug.Log("yes");
    19.         System.Threading.Thread.Sleep(time);
    20.         result[0] = 1f;
    21.     }
    22. }
    23.  
     
  2. hydos06

    hydos06

    Joined:
    Oct 18, 2019
    Posts:
    10
  3. hydos06

    hydos06

    Joined:
    Oct 18, 2019
    Posts:
    10
  4. hydos06

    hydos06

    Joined:
    Oct 18, 2019
    Posts:
    10
  5. hydos06

    hydos06

    Joined:
    Oct 18, 2019
    Posts:
    10
  6. hydos06

    hydos06

    Joined:
    Oct 18, 2019
    Posts:
    10
  7. hydos06

    hydos06

    Joined:
    Oct 18, 2019
    Posts:
    10
  8. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Not saying it's impossible, but that's not really what the job system is intended for. It was introduced to allow for comparably easy multithreaded code execution on native containers. The main intention is a ridiculous speedup, which can be achieved through the combination of jobs and the burst compiler.
    That said, the job system works with value types and internally with pointers. As such you need to give a job everything it needs to work - which includes the output array. So while you assign the time, you never assign the array, hence it being undefined and the exception telling you that.

    I'm not sure if you can make the worker thread of the job sleep tho. And i'm not sure what's your usecase. Sure, if you wait on the mainthread, then the whole program waits. However, if you do something / anything in a job and that takes x seconds to complete (including wait-time), and your main thread is waiting on that job, then now the entire program is waiting again. So i dont think this is what you actually want or need to do.

    If you want a method to be called after some amount of seconds, or repeatedly, then you can invoke it. If you want something to happen in regular intervals you could also use Coroutines (concurrent on the main thread, including waiting). Or you could "wait" by checking if some time condition is true each Update() call, thus only doing some action ABC after that time passed. I believe these are more likely what you are looking for.

    If you describe more closely what you actually need to do, then i or others may be able to help a bit more precicely. Chances are that it's a rather common problem and there are good solutions to it, and jobs just not being the right one.

    Hope this helps :)
     
  9. tim_jones

    tim_jones

    Unity Technologies

    Joined:
    May 2, 2019
    Posts:
    282
    The problem here is that the code creates the "result" variable, but it doesn't assign it to waitJob.result. If you do that, it will probably work (I haven't tested it).
     
    GamerLordMat likes this.
  10. hydos06

    hydos06

    Joined:
    Oct 18, 2019
    Posts:
    10
    thank you for the answer tim_jones it was helpful i have it sorted now
     
  11. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I told you the same thing tho.. plus a lot of extra stuff. If you wanted a one liner answer, say that.
     
    franklx2 likes this.