- Home /
To getComponent() or Instantiate ?
Ok I have been writing some scripts and they all interlink calling variables from each. The thing is that I never user getComponent(). I just instantiated the class (public Class1 class1;).
Is the code more efficient if I use getComponent instead of instantiating? If i'm just calling a variable should I just use getComponent?
$$anonymous$$ight not understand what your saying, but...
getComponent gives you a reference to the component your getting...and is intended for $$anonymous$$onobehaviors attached to a gameObject
if your just doing public $$anonymous$$yNon$$anonymous$$onoClass myNon$$anonymous$$onoClass = new $$anonymous$$yNon$$anonymous$$onoClass(); thats the correct way to do things.
Answer by Ryzokuken · Mar 07, 2013 at 07:05 AM
See...
You ARE kinda confused with this...
Both getComponent and Insantiate are used in VERY different scenarios.
When you are Instatiating an object, it means create an object "such as a projectile or any gameObject" usually, a prefab.
getComponent - means using the variable OR calculated value from ANOTHER script, to import the data for use in the script you are using.
This certainly means that both Instantiate AND getComponent are used in VERY different senses and NOT to be confused with each other
Hoping you understand...
~FORSAKEN
Answer by Norbyariel · Jul 28, 2017 at 06:25 PM
Good morning @Ryzokuken.
Excuse my English.
In my case I was assembling several objects that had systems of particles, in the explosion of barrels grouped.
That was expensive so I did a pull of several of these objects but in order to handle the particle systems (play and stop) I have to do a GetComponent of each of these objects. So I do a "play" and "stop" every time I need to use them again with a new group of barrels.
The question would be how much more or less expensive is my routine now?
thanks for your time.
Your answer
Follow this Question
Related Questions
GetComponent don't work (i am desperate) 0 Answers
How to call a function on an instance 1 Answer
Referencing in a prefab 1 Answer
How does instantiate connect to rigidbody?,How does instantiate userigidbody without getcomponent? 1 Answer
Getting instantiated clone's coordinates from the same script 1 Answer