- Home /
Problem was solved
can't reference variable in script
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.
Can you try making the CameraRotator public and just dragging and dropping in inspector?
I still don't understand why people keep using performance-heavy & inconvenient functions like Find(), but that's just an aside.
@nathanvj I tried to use something better like a SerializedField, but I wanted to get this working before I worried about performance enhancements.
Whats with the square brackets around CameraRig? Is it actually called "[CameraRig]"?
CameraRotater is a script, not a gameobject. It wont have a localRotation. If its a variable you've created yourself call it something else for safety.
Would you want
CameraRig.transform.localRotation
ins$$anonymous$$d?
How about you create rock solid references to things ins$$anonymous$$d of using this terrible "find object, find script" method. I know a lot of tutorials promote this kind of garbage coding, but seriously, even if you fix your problem, this is the worst possible way to do this.
I WILL NOT suggest a solution to your problem. Ins$$anonymous$$d, I suggest adding a singleton reference to your CameraRotator like this.
public static CameraRotator Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<CameraRotator>();
}
return instance;
}
private set
{
instance = value;
}
}
private static CameraRotator instance;
void Awake()
{
Instance = this;
}
Then your other code can just do this
CameraRotator.Instance.localRotation = blahRotation;
Answer by Positive7 · Feb 21, 2018 at 05:11 AM
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;
}
}
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
access instantiated object variable 0 Answers
How Do I Access and Change Items in a List on Another Script? 2 Answers
How to access variables from another script on collision ? 1 Answer