Search Issue Tracker

By Design

Votes

3

Found in

2019.4

2019.4.28f1

2020.3

2021.1

2021.2

2022.1

Issue ID

1344907

Regression

No

"The script '<script>' could not be instantiated!" error produced in build when a script creates an object that calls GC.Handle

IL2CPP

-

Steps to reproduce:
1. Open the attached project ("1344907.zip")
2. Open "SampleScene"
3. Make sure IL2CPP is selected as the current Scripting Backend (Edit -> Project Settings -> Player -> Scripting Backend)
4. Build and run the project
5. Open the Player logs (https://docs.unity3d.com/Manual/LogFiles.html)

Expected results: No errors are produced, script is instantiated
Actual results: "The script '<script>' could not be instantiated!" error is produced

Reproducible with: 2019.4.28f1, 2020.3.11f1, 2021.1.14f1, 2021.2.0b2, 2022.1.0a1
Didn't test on 2018.4 because couldn't find Player logs

Notes:
- The full scenario is that a MonoBehaviour script creates an object that calls GC.Handle for another object containing just a variable declaration
- Calling GC.Handle on the same class but in start instead of creating an object produces this error:
"ArgumentException: Object contains non-primitive or non-blittable data."
- Doesn't reproduce with Mono

  1. Resolution Note (2022.1.X):

    IL2CPP follows the documented behavior for GCHandle.Alloc with GCHandleType.Pinned - it will throw an ArgumentException if passed a non-blittable type.

Comments (2)

  1. VoodooDetective

    Jun 28, 2021 19:04

    I just wanted to add, the "non-blittable data" error is happening in that reproduction because the "B" class needs to have this attribute on it:

    [StructLayout(LayoutKind.Sequential)]

    That has nothing to do with the bug being reported though.

  2. VoodooDetective

    Jun 25, 2021 18:53

    This breaks "Programmer Instrument" in FMOD.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.