Home > Software design >  Transform.localRotation requires object reference, object the script is on already has a parent
Transform.localRotation requires object reference, object the script is on already has a parent

Time:08-07

The error. Thanks to Geeky Quentin for telling me.1I genuinely have no idea what's wrong, so I might as well post it here. The code works properly if it's a simple Transform.rotation, but I want to use Transform.localRotation specifically. The object with the code on it is already child to another object.

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

public class GunToCursor : MonoBehaviour
{    
    void Update()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        difference.Normalize();
        float rotation_x = Mathf.Atan2(difference.z, difference.y) * Mathf.Rad2Deg;
        Transform.localRotation = Quaternion.Euler(rotation_x, 0f, 0f);
    }
}

CodePudding user response:

It's caused by a typo, it is transform with a small t, not Transform

transform.localRotation = Quaternion.Euler(rotation_x, 0f, 0f);
  • Related