简体   繁体   中英

Trying to assign GameObject to variable in another script (Unity C#)

Note: Newer to Game Coding

I'm currently have my game setup that when you press a button it adds a prefab that includes all the traits a unit needs (such as movement, health, ect.). However that prefab is just the parent, it needs a child to be attached. That child is the actual different units. I was trying to call both the team and the child prefab in the function, however UI Buttons only allow for one parameter. As a work around I have the button call a function in the GameManager that Instantiates the parent prefab. In the GameManager function it also assigns that newly created GameObject to a variable defined in a different script. Then the button calls the second script and adds the child prefab to the parent prefab (I have not built this function yet). However, when trying to pass the parent GameObject to the second script, I get this error:

Assets\Scripts\GameManager.cs(27,9): error CS0120: An object reference is required for the non-static field, method, or property 'Spawn.parentObject'

Here is the snippet of the GameManager function:

public void Spawner(Team team)
    {
        Vector3 Orientation;
        if(team.FacingLeft)
        {
            Orientation= new Vector3(-1,1,1);
        }
        else
        {
            Orientation= new Vector3(1,1,1);
        }

        var obj =Instantiate(UnitPrefab, new Vector3(team.SpawnPosition.x,team.SpawnPosition.y,0), Quaternion.identity);
        obj.transform.localScale = Orientation;
        gEntity = obj.GetComponent<Entity>();
        team.AddUnit(gEntity);
        Spawn.parentObject=obj;
    }

And the snippet from the second script:

public class Spawn : MonoBehaviour
{
    public GameObject parentObject;
    public void UnitChild(GameObject unit)
    {
        
    }
}

You get this error because you are trying to access Spawn.parentObject directly from the class. However parentObject isn't static. You can only access it from an instance of Spawn . There are 2 options to fix this:

  • You could get a reference to an instance of Spawn . For example you could put

    public Spawn mySpawn; into your GameManager. Then, in the UnityEditor, you could drag an object that has the Spawn script attached into the slot called mySpawn on your GameManager. Now you could access that instance of Spawn like this:
    mySpawn.parentObject = obj;

  • Or you could make Spawn or at least Spawn.parentObject static.
    This means, that you wouldn't have instances of Spawn . You would be able to directly access Spawn.parentObject . You do this by changing your declaration of Spawn.parentObject to this:

     public static GameObject parentObject;

    Now you can access Spawn like you are doing it right now. In this case, you propably also want Spawn.UnitChild() to be static so that you easily access it too.

To decide which of these fixes you want to use, you need to think about what you want Spawn to be:

Do you want to have multiple Spawn s in your UnityScene and access them dynamically later? Or do you just want Spawn to be a little Helper class that you can use from anywhere easily?

It is quite hard to explain the difference between static and non- static but maybe this helps you to understand it: https://www.techopedia.com/definition/4913/static-c#

If you want to have a single instance of Spawn in your Scene, you might also look into Singletons: https://gamedevbeginner.com/singletons-in-unity-the-right-way/ They are basically a way to easily make sure that only one instance of a class exists and allow you to globally access this single instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM