Search code examples
c#unity-game-enginerotation

Unity, Changing the rotation of a child object. Cannot access the child object


I'm designing the logic for aiming and shooting from the player in a 2D game. When the user clicks and drags on the screen, it is supposed to rotate around the sprite a line formed by 3 dots with the same angle as the player dragged so he can aim. Like in a bubble shooter.

The "Player" prefab has a child Object called "Sight" which is a transform with 4 dots sprites as child objects with their corresponding offsets, so rotating "Sight" will rotate the whole line.

enter image description here

In each Update loop I look, for the sight to rotate it according to the mouse position but it seems that the variable sight is null. I tried to get the object by tag, and having a public variable so I can assign the prefab from the inspector. Also I tried to grab only the transform component. I'm sure that is a pretty dumb issue but I can't see it. I would appreciate any help.

Aiming logic:

public class PlayerShooting : MonoBehaviour {

    public GameObject BulletPrefab;
    public float coolDownTimer = 0.0f;
    public float aimDistance = 0.5f;

    private Vector2 originPoint = new Vector2 (0f,0f);
    private Vector2 endPoint = new Vector2(0f,0f);

    private bool dragging = false;
    public GameObject sight;

    // Update is called once per frame
    void Update () {

        // TOUCH DOWN
        if (Input.GetMouseButtonDown(0) == true) {
            originPoint = getPointInWorld(Input.mousePosition);
            endPoint = originPoint;
            dragging = true;
        }

        // TOUCH UP
        if (Input.GetMouseButtonUp(0) == true) {
            endPoint = getPointInWorld(Input.mousePosition);
            dragging = false;
            FireBullet();
        }

        // DRAGGING
        if (dragging) {


            endPoint = getPointInWorld(Input.mousePosition);
            // AIM
            // !!!: THIS IS THE ISSUE
            sight = GameObject.Find("Player/Sight");
            if (sight != null && endPoint != originPoint) {
                Vector2 dir = originPoint - endPoint;
                dir.Normalize();    
                float zAngle = Mathf.Atan2 (dir.y,dir.x) * Mathf.Rad2Deg - 90;
                Quaternion newRotation = Quaternion.Euler(0,0,zAngle);
                sight.transform.rotation = newRotation;
            }
            else
            {
                Debug.Log("Error: cannot aim because Sight was not instantiated");
            }
        }
    }

    void FireBullet (){

    }

    Vector2 getPointInWorld (Vector3 point){
        Vector2 pos = Camera.main.ScreenToWorldPoint(point);
        return pos;
    }
}

Solution

  • After taking a step back it seems that the no object could be assigned to the variable sight because the scene file was corrupt. It was clonned from a Bitbucket repository. The original approach Works fine and the instruction

    sight = GameObject.Find("Player/Sight");
    

    is able to find the object. I decided to assign it from the script and not from the editor so I can enable and disable the sight if the player is shooting or not.