I am trying to get access to the ‘localRotation’ variable inside script ‘CameraRotater’. This should be easy but when I type Vector3 rot = GameObject.Find("[CameraRig]").GetComponent<CameraRotater>().localRotation;
I get an error saying that ‘CameraRotater could not be found’. However when I type Vector3 rot = GameObject.Find("[CameraRig]").GetComponent("CameraRotater").localRotation;
I get a different error saying that ‘localRotation could not be found’. Here is the code for ‘CameraRotater’ public class CameraRotater : MonoBehaviour { public Vector3 localRotation; }
. I have looked at other peoples answer to this very question but none of their solutions have worked for me. Any help would be greatly appreciated.
For C# look at this: referencing an object
You got the ‘CameraRotater could not be found’ error because the CameraRotater is not in the same directory as the one you’re looking from it lets say GetCameraRotater.cs.
If GetCameraRotater.cs is inside Scipts folder or namespace but CameraRotater.cs is inside BlaBla folderor namespace you have to add a using to GetCameraRotater using BlaBla;
GetCameraRotater .cs :
using BlaBla;
using UnityEngine;
public class GetCameraRotater : MonoBehaviour
{
private void Start()
{
var rot = GameObject.Find("[CameraRig]").GetComponent<CameraRotater>().localRotation;
Debug.Log(rot);
}
}
CameraRotater.cs
namespace BlaBla
{
using UnityEngine;
public class CameraRotater : MonoBehaviour
{
public Vector3 localRotation;
private void Awake()
{
localRotation = transform.localEulerAngles;
}
}
}