Skip to content

IScript null during Compilation Success Handler #3

Description

@brentshermana

Here, I use your library to attach a script to a game object. First I compile the type of my component, then I compile code which adds that component to the current gameobject. Inside my success handler (where I would expect the script to be instantiated), the script is null, but it's not null almost immediately after, during the first call to Update().

If this is expected behavior, a comment or two pointing this out (and why it occurs) would be very helpful!

Thank you for sharing your library.

The Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UCompile;

public class UCompileComponentTest : MonoBehaviour {

    IScript addComponentScript;

    // Use this for initialization
    void Start() {
        CSScriptEngine engine = new CSScriptEngine();
        engine.AddUsings("using UnityEngine;");
        string componentCode = @"
            public class BasicComponent : MonoBehaviour {
                void Start () { Debug.Log(""Hello, UCompile!""); }
            }
        ";
        string addComponentCode = @"
        GameObject.Find(" + "\"" + gameObject.name + "\"" +  @").AddComponent<BasicComponent>();
        ";
        System.Type componentType = engine.CompileType("BasicComponent", componentCode);
        engine.AddOnCompilationSucceededHandler(OnCompilationSuccess);
        engine.AddOnCompilationFailedHandler(OnCompilationFail);
        addComponentScript = engine.CompileCode(addComponentCode);
	}

    void OnCompilationSuccess(CompilerOutput output)
    {
        Debug.Log("compilation success");
        Debug.Log("script is null: " + (addComponentScript == null) );
    }

    void OnCompilationFail(CompilerOutput output)
    {
        Debug.Log("Fail");
        for (int i = 0; i < output.Errors.Count; i++)
            Debug.LogError(output.Errors[i]);
        for (int i = 0; i < output.Warnings.Count; i++)
            Debug.LogWarning(output.Warnings[i]);
    }

    // Update is called once per frame
    float time = 0f;
	void Update () {
		if (addComponentScript != null)
        {
            Debug.Log("Not null after " + time + " seconds");
            addComponentScript.Execute();
            addComponentScript = null;
        }
        else
        {
            time += Time.deltaTime;
        }
	}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions