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.
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.
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.
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)
{
// ...
}
}
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!