Other
Difference between GetComponent
What is the difference between
Script sc = GetComponent<Script>();
and
Script sc = Script();
thanks so much!
Answer by $$anonymous$$ · Mar 04, 2017 at 11:17 AM
Script sc = GetComponent<Script>();
will get the component of a gameobject (Script is not a unity type)
for Example
public GameObject FluffyBunny;
FluffyBunny.GetComponent<BunnyAI>().GetCuteness();
will return the Cuteness of a bunny from their bunnyAI script..
Not entirely sure what you mean by the second part though... Cause from just testing script is not a useable type in unity api Either that or its a custom class type...
Answer by hexagonius · Mar 04, 2017 at 11:18 AM
They have nothing in common.
Is the correct way to get a component. It's a generic method call.
won't compile (have you even tried?).
The left hand side is declaring a variable of type script. The right hand side is either that classes constructor, or a simple method. If it's a constructor, the new keyword is missing. It's also wrong, because that would ideed create a new Script instance, but none Unity would be aware of and especially none that's attached to any gameobject, so it's useless.
If that was a method, it cannot be declared in the Script class because of the name's ambiguity.
If it was in another class it's return type would need to be of type Script.
"have you even tried?" - that was a lil rough.. maybe try removing that part.. no need to attack people.