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
0
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 which is ingerited from by 3 other kinds of bullets. In this prant bullet script, I have it read from a bool in the player's script which 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 this script and its children 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, which has its own script checks the player's direction itself when executing Start().

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.

So what i'd like to know: Is there a way to accomplish this 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
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 RudyTheDev · Sep 20, 2015 at 12:36 PM

There are various solutions to this. 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
Add comment · 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 : $$anonymous$$onoBehaviour
 {
     public bool facingRight;
 
     public void ShootBullet()
     {
         new GameObject().AddComponent<Bullet>().Initialize(facingRight); // actual prefab logic would go here
     }
 }
     
 public class Bullet : $$anonymous$$onoBehaviour
 {
     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.

Follow this Question

Answers Answers and Comments

29 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

Related Questions

Changing Variables on one script with multiple other scripts 1 Answer

GetComponnent null reference C# 1 Answer

Randomized array result from other scipt 2 Answers

Referenced Bool is not updating 1 Answer

Automatic Switching of two or more booleans in Inspector 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