I am new to Unity but I know that if an object is a child, then it will follow the parent and have the same rotations; however, I am creating a top-down, 2d, mobile, naval warfare game and I wanted to instantiate the cannonball prefab with the push of the button (that works) and then in a script on the cannonball prefab I had the cannonball check what the tag of its parent was (left side cannon or right side cannon) to know whether to move left or right (with transform.Translate). Basically, I wanted to know if I could make the cannonball move independently from the parent while still being a child of that parent.
More info: I have a ship game object that has 4 children (right side cone of fire, left side cone of fire, right side cannon game object to spawn the cannonballs, and left side cannon that does the same), the lines that have R_Cannon, L_Cannon, etc. are the cannons on the fort that the player is shooting, also whenever I look at a fort cannon gameObject and I press the shoot button, it gives me the error “NullReferenceException: Object reference not set to an instance of an object
CannonFire.Update () (at Assets/Scripts/CannonFire.cs:37)” line 37 is the one that has == “R_Cannon” .
Here’s the code:
public class CannonFire : MonoBehaviour {
[SerializeField] private float canBallSpeed = 2f;
[SerializeField] private GameObject explosionPrefab;
[SerializeField] private GameObject ripplePrefab;
[SerializeField] private GameObject ship;
private float fortCannonDamage = 10;
private bool rSideFired = false;
private bool lSideFired = false;
// Use this for initialization
void Start () {
StartCoroutine(CannonballLifespan());
if (ship.GetComponent<PlayerBehavior>().rSideCanball) {
gameObject.transform.eulerAngles = ship.transform.GetChild(2).transform.eulerAngles;
rSideFired = true;
}
if (ship.GetComponent<PlayerBehavior>().lSideCanball) {
gameObject.transform.eulerAngles = ship.transform.GetChild(3).transform.eulerAngles;
lSideFired = true;
}
}
// Update is called once per frame
void Update () {
if (gameObject.transform.parent.name == "R_Cannon") {
transform.Translate(Vector2.right * Time.deltaTime * canBallSpeed);
}
if (gameObject.transform.parent.name == "L_Cannon") {
transform.Translate(Vector2.left * Time.deltaTime * canBallSpeed);
}
if (gameObject.transform.parent.name == "U_Cannon") {
transform.Translate(Vector2.up * Time.deltaTime * canBallSpeed);
}
if (gameObject.transform.parent.name == "D_Cannon") {
transform.Translate(Vector2.down * Time.deltaTime * canBallSpeed);
}
/*if (gameObject.transform.parent.tag == "Right Side Cannons") {
gameObject.transform.parent = null;
transform.Translate(Vector2.left * Time.deltaTime * canBallSpeed);
}
if (gameObject.transform.parent.tag == "Left Side Cannons") {
gameObject.transform.parent = null;
transform.Translate(Vector2.right * Time.deltaTime * canBallSpeed);
}*/
if (rSideFired == true)
{
RightSideFired();
}
if (lSideFired == true)
{
LeftSideFired();
}
}
void RightSideFired ()
{
transform.Translate(Vector2.left * Time.deltaTime * canBallSpeed);
}
void LeftSideFired ()
{
transform.Translate(Vector2.right * Time.deltaTime * canBallSpeed);
}