简体   繁体   中英

How do I make sure that Unity can find my game object without it returning as Null? Problem is with initializing the object before calling it

I am using the Transform function in Unity inorder to rotate my gun in my 2d top down shooter when the mouse is aimed in a certain direction. However, the problem is that Unity returns one of my functions as Null and therefore says that the game object can't be found. I have done some debugging and found that my Debug.Log(aimTransform); come back as null.

The code goes like this;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class PlayerAimWeapon : MonoBehaviour
{
    public static Vector3 GetMouseWorldPosition()
    {
        Vector3 vec = GetMouseWorldPositionWithZ(Input.mousePosition, Camera.main);
        vec.z = 0f;
        return vec;
    }
    public static Vector3 GetMouseWorldPositionWithZ()
    {
        return GetMouseWorldPositionWithZ(Input.mousePosition, Camera.main);
    }
    public static Vector3 GetMouseWorldPositionWithZ(Camera worldCamera)
    {
        return GetMouseWorldPositionWithZ(Input.mousePosition, worldCamera);
    }
    public static Vector3 GetMouseWorldPositionWithZ(Vector3 screenPosition, Camera worldCamera)
    {
        Vector3 worldPosition = worldCamera.ScreenToWorldPoint(screenPosition);
        return worldPosition;
    }

    private Transform aimTransform;
    
    private void Start()
    {
        Debug.Log(aimTransform);
        aimTransform = transform.Find("Aim");
        
    }

    private void Update()
    {
        Vector3 mousePosition = GetMouseWorldPosition();

        Vector3 aimDirection = (mousePosition - transform.position).normalized;
        float angle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg;
        aimTransform.eulerAngles = new Vector3(0, 0, angle);
        Debug.Log(angle);
    }
}

The main problem I think is below.

 private Transform aimTransform;
    
    private void Start()
    {
        Debug.Log(aimTransform); <--------------- This comes back as Null which is the problem. 
        aimTransform = transform.Find("Aim"); <-------- Aim is just the object in my game (Gun)
        
    }

I have spent some time figuring out how to Initialize the object and then call it using the Find function so it doesn't come back as Null but I haven't been successfull.

Once unity can find the Object, it should work fine with the mouse pointer code I have further below.

As said before, I debugged the Null from the aimTransform and it returns as null. I'm not sure how to fix it and allow Unity to actually find my game object and let it be transformed. I also know that the main issue is that the object hasn't been initialized properly and I don't really know how to do it. Thanks.

Problem in aimTransform field, it's not initialized and you try to get transform with name "Aim" from null field. Transform.Find() search transform by name in it's childs. If you want to find gameobject with name "Aim" you have several ways:

  • If you have possibility you can serialize field aimTransform and drag and drop reference from Inspector.
  • You can call GameObject.Find("Aim") to find gameobject by name, but it will work only in case if gameobject with apropriate name exists on your scene.

The correct code would be this:

private void Start()
{
    aimTransform = transform.Find("Aim"); // <-- assuming you don't mean
                                          // GameObject.Find() at which point
                                          // you'd need to change the type of
                                          // aimTransform as well :)
    Debug.Log(aimTransform);
}

aimTransform is not yet "set", therefore, it's null. First assign it, then you can log it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM