Unity, C# and OOP
i have a generic question that might be really dumb, but i dont get it.
im writing code for a generic turret to shoot a generic missile. i want the turret to hold an array (public, set by me for now) of characteristics, and i want it to use this array to initialize the behaviour script on the missile.
so i got a turret object with a turretScript: in this script there is a public variable "array". the turret instantiates a missile prefab that has a missileScript attached. i want this script's own "array" (also public) to be initialized from the turretScript that instantiates the missile:
approach 1.
Instantiate(missile, otherParameters); missileScript ms = missile.transform.GetComponent(); ms.initializeMissile(array);
as in my prevois experience with OOP i expected this to work like this: instantiate a missile with his script attached to it, then access THAT INSTANCE of the missileBrain class, then change the internal values of THAT INSTANCE. instead, every time initializeMissile() was called, it changed the values in the "prototype" class in my assets folder. thus changing the variables for every successive missile spawned. (this ofc caused a massive lot of problems).
approach 2. learning from the prevois result, i cahnged it to be the same as approach 1, but the instantiate() is called AFTER getcomponenet() and initialize(). and this works. every time ANY missile is launche by ANY turret, the missileScript class is changed, and therefore i have to change it back when i want to launch a completely standard missile.
now finally the question: is this intended? if it is... WHY is this intended? isnt one of the points of OOP the fact that you can work on a single instance of class Something whithout affecting the "prototype" for all future objects of type Something? or am i doing something wrong and im actually telling the script to change the class definition instead of the specific instance?
i hope ive been clear, although i probably have not. im sorry fot the long post but...
Answer by HarshadK · Apr 05, 2016 at 09:36 AM
The approach 1 is almost right but with a slight issue.
You can instantiating a new missile using your missile
object and then you are getting the reference to missileScript
from your missile object and not the newly instantiated missile
. Your approach should be:
// We instantiate a missile and get a reference to it
GameObject newMissile = (GameObject) Instantiate (missile, otherParameters);
// Now get the missileScript from this newly instantiated missile
missileScript ms = newMissile.transform.GetComponent();
ms.initializeMissile(array);
Now here you are accessing the missileScript from the newMissile and not your prototype missile.
We should probably add that you can define your "missle" prefab variable to be of type of your "$$anonymous$$issleScript". When you instantiate that prefab, Instantiate will return that component on the instantiated object:
public $$anonymous$$issleScript misslePrefab;
// [...]
$$anonymous$$issleScript instance = ($$anonymous$$issleScript)Instantiate(misslePrefab, pos, rot);
instance.initialize$$anonymous$$issile(array);
That way you don't need to use GetComponent as Instantiate does this for you. $$anonymous$$eep in $$anonymous$$d that Instantiate always return the same type that you pass in. If you pass in a component, the whole gameobject where this component is attached to is cloned as well. However the return type is always the type you passed in.
ps: $$anonymous$$eep in $$anonymous$$d when you change the variable type of your prefab variable you have to reassign it in the editor.
alright, i am officially retarded. thats the "standard" procedure i used like dozens of times, i really dont know why i didnt notice that i was doing things differently for some reason.
thanks!
Your answer
Follow this Question
Related Questions
Rotate character 0 Answers
How do i fix this problem 1 Answer
Help with a C# script .SetActive 1 Answer
How to add animations to character ? 0 Answers
AddForce Isn't working? 1 Answer