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.
CodePudding user response:
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.
CodePudding user response:
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.