Trying to assign GameObject to variable in another script
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)
{
}
}