- Home /
GameObject.Find() GetComponent() based assigns vs others?
This is a theory question of sorts, where I am trying to learn more about different types of programming languages and their structure.
In Unity, the GameObject.Find() and GameObject.GetComponent() are used to designate objects - what is this kind of system called, and what are some other systems? How do they compare to each other? Is Unity's Find, GetComponent and such more preferable than others? How might Unity be improved to not use Find, GetComponent?
It's a question in need of answers, though an open-ended one. $$anonymous$$y the "community wiki" checkbox isn't enabled on this stack...
Answer by kennypu · Apr 11, 2011 at 08:28 PM
Although I do not know how the underlying code works for those methods, those are basicly known as 'setters' and 'getters', or mutators and accessors. Its part of what is known as Object Oriented Programming (OOP). As far as performance goes, it is usually better to directly access those properties, rather than accessing it with the likes of getters and setters, but that can cause problems as well.
Answer by scriptocalypse · Apr 11, 2011 at 09:02 PM
In my admittedly limited experience, I recently smashed several tutorial projects together and did a lot of code conversion to C#. In doing so I noticed a disturbing amount of code that looks like this:
//In every update cycle.... void Update(){ GameObject foo = GameObject.Find("Foo"); BarComponent bar = foo.GetComponent<Bar>();
GameObject[] gos = GameObject.FindWithTag("AllFoos");
}
My smashup, when tested on my Droid 2 was getting something on the order of 18fps on average after a certain point. When the object graph was relatively small, I didn't have nearly so many problems with performance. So what I decided to do was pre-cache those values in void Start() instead of accessing them every update I immediately shot up to an average of 25fps. It really makes a difference, and the more gameobjects you have in your scene the bigger the difference will be.
Your answer
Follow this Question
Related Questions
Finding things with Raycasthit 1 Answer
Referencing to an initiated Text 1 Answer
Need an advise, how to find all objects with a specific name and a specific script? 2 Answers
Instantiated Prefab using its transform and object on other scripts 1 Answer
Better to assign components to variables or reference directly each time 3 Answers