I'm trying to access a method in another script without using the keyword "static";
public void CreateFarm()
{
new Farm().NewResourceBuilding(gameObject);
}
My question is; will Unity automatically delete this object once it's done calling the method? Or will I need to manually destroy it? If the latter, how will I go about that?
I've searched on Google and been on this problem for a few days until I've finally come to this solution lol. So annoying to reference a static yet want to add a gameobject via the inspector.
Edit (Left the previous content untouched as some of answers already provided for the above might be useful to others). I have since revised my code and the flow.
I have a scene with a number of "empty lots" which I use images as placeholders childed to a Canvas.
The following steps occur:
Clicking on any of these images calls Unity's event system to open a panel, from the onclick options of the inspector. This panel will allow me to select from a number of different buildings to construct.
ChangePanels.CS
public void RuralBuildingSelected()
{
empirePanel.SetActive(false);
characterPanel.SetActive(false);
ruralBuildingPanel.SetActive(true);
}
Why I wanted to avoid "static" was because no static method is callable from the inspector (AFAIK). I could have created my own script and called a public onclick, but perhaps one of you might be able to advise which option of these is more efficient.
Whichever "emptylot" I clicked on, I wanted them to send through their data into a manager script so whatever building I built, could replace the emptylot.
EmptyLot.CS
public void CreateFarm()
{
new GameManager().GetEmpty(gameObject);
}
The following follow gives you an option of a number of buildings to construct. If I click on the farm, I need to;
- Instantiate a new Farm
- Parent it to the emptyLot's parent
- To replace the emptylot with the new farm, I set them to have the same positions
- Destroy the emptylot that called the new farm into existence
- Bring back the main panel
Farm.CS
new public void NewResourceBuilding(ResourceBuildings resource)
{
Farm newFarm = Instantiate(resource) as Farm;
newFarm.transform.SetParent(GameManager.emptyLot.transform.parent);
newFarm.transform.position = GameManager.emptyLot.transform.position;
newFarm.levelText = newFarm.GetComponentInChildren<Text>();
newFarm.name = resource.name;
Destroy(GameManager.emptyLot);
GameObject main = GameObject.Find("Main Canvas");
main.GetComponent<ChangePanels>().MainScene();
}
My Questions:
The "EmptyLot.CS" script creates a new GameManager to call a public method. I don't reference the object. Will this automatically be added to the garbage collector or should I just destroy it after using it? If I need to destroy it manually, how would I go about that?
I note that if I went ahead with the static methods and implement my own OnClick() method I can condense this to just two scripts instead of the three. So should I do that? Or based on the above code, is it just as efficient to keep it the way it is? I'm not a pro at this, but inline with Wondra's comment (to Almo's answer) it does seem like the approach of creating objects to delete instantly is a performance issue?
Thank you for taking the time to read/help/advice
gameObject
reference you're passing it? If you don't actually need a persistent instance of Farm, it's likely that you can refactor your code to avoid allocating one in the first place. \$\endgroup\$