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 /
avatar image
0
Question by Bouldoro · Jul 05, 2018 at 04:58 AM · instantiatevector2

How do I get an object to inherit "something" from another game object that it has no parent/child relationship to?

So I have my gun and bullet. The bullet is not a child of the gun. My gun has a parent with a target. I need the bullet to have the same target without referencing the target itself.

The gun does this on a condition

 Instantiate (attack, attackPosition.position, Quaternion.identity);

The bullet does this when instantiated

 rb.MovePosition (rb.position + targetVector.normalized * Time.deltaTime * attackSpeed);

The targetVector is on the gun's parent.

Comment
Add comment · Show 1
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 JVene · Jul 07, 2018 at 04:15 PM 0
Share

There's a simple method by which to think of these things. Inheritance requires one to say that the subject "IS-A" thing. In this case, a bullet is not a gun, so inheritance would not seem to be the correct way of thinking. When the answer to "is it a...." is no, avoid inheritance. Ins$$anonymous$$d, when the answer to the question "does it have a..." is yes, this suggests containment, not inheritance. The gun will "have a" bullet, but the bullet will not "have a" gun, it will be owned by a gun. The relationship isn't a parent child, but a container and an element which it contains. This means PizzaPie's idea of having the gun class instantiate a bullet and contain it is on track. This "is-a" or "has-a" verbiage is from older computer science basics, and it's a classic.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PizzaPie · Jul 05, 2018 at 10:03 AM

You could pass directly the targetVector after the Instatiation (inside the Gun Script) ex.

     //Gun class
     GameObject bullet = Instantiate (attack, attackPosition.position, Quaternion.identity);
     bullet.GetComponent<Bullet>().Init(targetVector);
     
     //Bullet class
     
     private bool isSet = false;
     private Vector3 targetVector;
    
     public void Init(Vector3 targetVector){
          this.targetVector = targetVector;
          isSet = true;
     }
 
    void FixedUpdate(){
             if(!isSet)
                    return;
            rb.MovePosition(); // as you have it 
           }


It would be better if it could be passed via the Constructor but Unity won't allow custom constructors on MonoBehaviours. Fixed Cheers

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 Bouldoro · Jul 07, 2018 at 08:56 AM 0
Share

What do you mean by the last part? $$anonymous$$y rb.$$anonymous$$ovePosition is usually in FixedUpdate anyway.

avatar image PizzaPie Bouldoro · Jul 07, 2018 at 03:52 PM 0
Share

That was a terrible answer fixed it. Not sure if FixedUpdate is called between the Instatiate and the GetComponent thus the use of the isSet flag, if it isn't (called) it is useless. Also it can be done with a propery ins$$anonymous$$d of a function.

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

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating objects at different locations 0 Answers

Checking if object intersects? 1 Answer

toss up game object into air with freefall physics 1 Answer

why is Transform, KeyCode, Vector2 and Instantiate does not exist in the current context? 1 Answer

Not able to fire projectiles at mouse position 2 Answers


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