I'm trying to access a method in another script without using the keyword "static";
public void CreateFarm()
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.
public void RuralBuildingSelected()
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.
public void CreateFarm()
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
new public void NewResourceBuilding(ResourceBuildings resource)
Farm newFarm = Instantiate(resource) as Farm;
newFarm.transform.position = GameManager.emptyLot.transform.position;
newFarm.levelText = newFarm.GetComponentInChildren<Text>();
newFarm.name = resource.name;
GameObject main = GameObject.Find("Main Canvas");
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