Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by riccardo.cantoni · Apr 05, 2016 at 08:32 AM · scripting problemscriptingbasicsinstantiating

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 05, 2016 at 11:14 AM 1
Share

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.

avatar image riccardo.cantoni · Apr 05, 2016 at 11:49 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges