• 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
Question by qwijnhard · Sep 21, 2015 at 09:32 AM · c#getcomponentshootingparentboolean

Reading a var from variable parents

Right now i have a bullet script w$$anonymous$$ch is ingerited from by 3 other kinds of bullets. In t$$anonymous$$s prant bullet script, I have it read from a bool in the player's script w$$anonymous$$ch direction the player is facing to base the bullet's direction off of.

Now that i have to create an enemy script that is able to shoot the same bullets, i have come across my problem; the Bullet script uses

 GameObject thePlayer = GameObject.Find("Player");  
 PlayerController PlayerScript = thePlayer.GetComponent<PlayerController>();  
 if(!PlayerScript.facingRight)  
 {  
 speed = speed*-1;  
 }

So if an enemy wants to fire, i'd have to write an identical copy of t$$anonymous$$s script and its c$$anonymous$$ldren and substitute PlayerController with EnemyController if i want it to work the same way.

The Shoot() method in the PlayerController script instantiates a bullet at the player's position, then checks the player's direction specifically and moves that way.

in short, the bullet GameObject, w$$anonymous$$ch has its own script checks the player's direction itself when executing Start().

How i understand t$$anonymous$$s code is that as soon as the bullet is spawned, it looks around the world for the player's facing direction, then moves in that direction.

what i want to change it into is that as soon as the bullet is spawned, it asks "who shot me?" and then looks for the facing direction of the prefabbed instance that shot the bullet.

the "Who shot me?" is the essence of my problem, really.

So what i'd like to know: Is there a way to accomplish t$$anonymous$$s by saying "just get facingRight from whoever shot you, nevermind who that is" ?

Because that would really make my day.
i could provide you the script if needed.

Edit: fixed poor wording and added more info.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by RudyTheDev · Sep 20, 2015 at 12:36 PM

There are various solutions to t$$anonymous$$s. You definitely don't have to write an identical script (always a bad idea in programming), you can inherit both player and enemy from the same script, for example:

 using UnityEngine;
 
 public abstract class Controller : MonoBehaviour
 {
     public bool facingRight;
 }
 
 public class PlayerController : Controller
 {
 }
 
 public class EnemyController : Controller
 {
 }
 
 public class BulletShooter : MonoBehaviour
 {
     private void Update()
     {
         Controller controller = gameObject.GetComponent<Controller>();
         Debug.Log("Facing right = " + controller.facingRight);
     }
 }

If your individual controllers have vastly different direction logic or already inherit different classes, you might use interfaces instead:

 public interface IController
 {
     bool IsFacingRight();
 }
 
 public class EnemyController : MonoBehaviour, IController
 {
     private int direction;
 
     public bool IsFacingRight()
     {
         return direction == 0;
     }
 }
 
 public class PlayerController : MonoBehaviour, IController
 {
     private bool facingLeft;
 
     public bool IsFacingRight()
     {
         return !facingLeft;
     }
 }
 
 public class BulletShooter : MonoBehaviour
 {
     private void Update()
     {
         IController controller = gameObject.GetComponent<IController>();
         Debug.Log("Facing right = " + controller.IsFacingRight());
     }
 }

There are other less elegant ways, but the above two are the typical methods to share functionality between classes.

Comment
Chom1czek

People who like this

1 Show 3 · 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 qwijnhard · Sep 20, 2015 at 03:00 PM 0
Share

Thanks! I didnt even think about them both inheriting from something like an "Actor" script. it seems i lost sight of the forest because of all the trees in the way, heheh.

Edit: it seems i jumped the gun - and i did not provide enough information to boot. i'll update the topic but for now i want to add that

The Shoot() method in the PlayerController script instantiates a bullet at the player's position, then checks the player's direction and moves that way.

in short, the bullet GameObject, which has its own script checks the player's direction itself when executing Start(). i could provide you the script if needed.

How i understand this code is that as soon as the bullet is spawned, it looks around the world for the player's facing direction, then moves in that direction.

what i want to change it into is that as soon as the bullet is spawned, it asks "who shot me?" and then looks for the facing direction of the prefabbed instance that shot the bullet.

the "Who shot me?" is the essence of my problem, really.

avatar image RudyTheDev qwijnhard · Sep 20, 2015 at 04:40 PM 0
Share

The typical method is to tell any created thing what it needs to know (rather than the created thing trying to find this out). In this case, the bullet wouldn't even need to know about the shooter (in fact, that's a good idea when the shooter could die while the bullet is still on its way). Here's a simple way:

 public class Player : MonoBehaviour
 {
     public bool facingRight;
 
     public void ShootBullet()
     {
         new GameObject().AddComponent<Bullet>().Initialize(facingRight); // actual prefab logic would go here
     }
 }
     
 public class Bullet : MonoBehaviour
 {
     public void Initialize(bool facingRight)
     {
         // ...
     }
 }
 
avatar image qwijnhard RudyTheDev · Sep 20, 2015 at 06:15 PM 0
Share

i see. make it so it doesn't even need to look in the first place. now i can fix it for sure, hahah! 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

Changing Variables on one script with multiple other scripts 1 Answer

How to get bullet hole to wrap around corners of objects? 0 Answers

How do I change an objects script variable in my scene? 2 Answers

Can’t keep the position 0 Answers

Upcast vs GetComponent 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