Cannot reference GameObject components through plugin

hiiii - as the topic suggests - while tinkering with creating a plugin, there was one specific problem faced. If I created variables and then referenced them to equal components (such as MeshFilters and MeshRenderers [yes, I used the proper variables]), the values would always return as null specifically in-game, though refencing the actual GameObject holding the components would work. It would actually give the GameObject variable the value, but something like the MeshRenderer variable would not be assigned anything. On the other hand, it works perfectly fine in Unity, it only fails in the actual game. Trying to add the components through script will give an error that they are already there, but then trying to get the components will give an error acting like they don’t exist. This was tested on a placeable Barricade prefab.

Basically, the plugin function works perfectly with no problems in Unity, it works in-game only for assigning the actual GameObject to a variable, but fails when doing the components. I want to be able to access these components in-game, and want to know why they suddenly become inaccessible.

Disclaimers and Clarifications:

  • I’m not bundling the actual scripts, I add them separately as a plugin
  • I tested through multiple ways of assigning, like the public variable drag-and-drop thing, the manual script GetComponent stuff
  • The plugin is functional and recognizes the components are there, both in-game and in Unity editor, but seems to be unable to access any of its other info only in-game

Can you share a code example of what you’re trying to do, please?

(Not the overall thing I’m trying to do, just a snip of the test script to see what works before dedicating myself to writing a full project, so I don’t spend time writing something that doesn’t work.) meshFilter and gameObjManual are tests using referencing in the script, meshRenderer and gameObj are ones using that drag and drop thing:

public GameObject gameObj;
public GameObject gameObjManual;
public MeshFilter meshFilter;
public MeshRenderer meshRenderer;
void Start()
{
gameObjManual = GameObject.Find(“Model_0”);
meshFilter = GetComponent();

    if (gameObj == null)
    {
        Console.WriteLine("gameObj is EMPTY");
    }
    else if(gameObj != null)
    {
        Console.WriteLine("gameObj is " + gameObj.name);
    }

    if (gameObjManual == null)
    {
        Console.WriteLine("gameObjManual is EMPTY");
    }
    else if (gameObjManual != null)
    {
        Console.WriteLine("gameObjManual is " + gameObjManual.name);
    }

    if (meshFilter == null)
    {
        Console.WriteLine("meshFilter is EMPTY");
        meshFilter = gameObj.GetComponent<MeshFilter>();

        if (meshFilter == null)
        {
            Console.WriteLine("meshFilter is EMPTY AGAIN");
            meshFilter = gameObjManual.GetComponent<MeshFilter>();

            if (meshFilter == null)
            {
                Console.WriteLine("meshFilter is STILL EMPTY");
                meshFilter = GameObject.Find("Model_0").GetComponent<MeshFilter>();

                if (meshFilter == null)
                {
                    Console.WriteLine("meshFilter is EMPTY (final)");
                }
                else if (meshFilter != null)
                {
                    Console.WriteLine("meshFilter was fOuNd");
                }
            }
            else if (meshFilter != null)
            {
                Console.WriteLine("meshFilter was fOuNd");
            }
        }
        else if (meshFilter != null)
        {
            Console.WriteLine("meshFilter was fOuNd");
        }
    }
    else if (meshFilter != null)
    {
        Console.WriteLine("meshFilter was fOuNd");
    }

    if (meshRenderer == null)
    {
        Console.WriteLine("meshRenderer is EMPTY");
    }
    else if (meshRenderer != null)
    {
        Console.WriteLine("meshRenderer was fOuNd");
        GameObject.Find("Model_0").GetComponent<MeshRenderer>().materials[0].color = new Color(255, 255, 0);
        Console.WriteLine("Color change test success");
    }
}

Top image shows up in the log once placing the barricade. Bottom is the Unity part for the separate bundle so I can assign the values, and re-add the script separately through plugin. During run-time in Unity (not in-game), the 2 empty values are assigned components, and the barricade turns yellow. In previous tests where I still try to change the color regardless, in-game, it would give an error that the variable wasn’t set to an instance of an object (and wouldn’t change color).
PublicAssign

Components that only clients need like mesh filter, mesh renderer, audio source, etc are automatically removed on the server. If absolutely necessary you can provide a “Clip” prefab that isn’t modified by the server.

That said, it looks like you are trying to implement barricade colors via server plugin which, unfortunately, isn’t possible because those changes wouldn’t be replicated to clients, sorry.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.