- Home /
The question is answered, right answer was accepted
c# public variables not appearing in inspector
Hey there, I'm just trying to make a simple script that lets a camera follow a character since parenting the camera doesn't work. For whatever reason, none of the variables appear in the editor, so naturally it runs into issues since I can't drag the character into the variable slot. Anyone know what's going on? Here's the code:
using UnityEngine;
using System.Collections;
public class cameraTracking : MonoBehaviour {
public GameObject targetl;
public float xOffset = 0;
public float yOffset = 0;
public float zOffset = 0;
void LateUpdate() {
this.transform.position = new Vector3(target.transform.position.x + xOffset,
target.transform.position.y + yOffset,
target.transform.position.z + zOffset);
}
}
O$$anonymous$$ I formatted your code for you as you're new but be careful to do it correctly with your next question.
Have you dragged the script onto a GameObject?
Try: Right clicking on the script in the Project tab and click reimport/refresh $$anonymous$$ake sure it's on a GameObject Do you have any editor scripts overriding it?
If you have errors in monodevelop or whatever other IDE you're using the inspector won't update itself, usually I like to comment out all code but my variables in VS in a few keys (CTRL $$anonymous$$ + C), not sure the command for doing so in $$anonymous$$onodevelop. Basically, comment out your errors and let it update in the inspector then go back to your code.
This isn't always the case but it tends to be a niggly problem that catches out even the experienced Unity users.
Answer by maccabbe · Feb 28, 2015 at 05:27 PM
The reason the variables are not being displayed is because there is a compile time error in your script because LateUpdate is referring to the variable target when you only declare a variable named target1. Use the following:
using UnityEngine;
using System.Collections;
public class cameraTracking : MonoBehaviour {
public GameObject target;
public float xOffset = 0;
public float yOffset = 0;
public float zOffset = 0;
void LateUpdate() {
this.transform.position = new Vector3(target.transform.position.x + xOffset,
target.transform.position.y + yOffset,
target.transform.position.z + zOffset);
}
}
Thanks maccabbe, that's got it working properly. To everyone else who answered, thanks for the help! And thanks for putting up with the new guy. By what do you mean formatting my code? I don't want to make the same mistake in what is likely to be a long list of other questions.
You had formatted some of the code but only in places making it difficult to read, just highlight all the code and hit the 101010 button to format, seemed to be in moderation for a long time but then Answers is run by volunteers so there are only ever a few people on here able to moderate.
$$anonymous$$ake your question as easy to answer as possible and always search for answers first as it's always quicker.
That said the fact you're asking how to post correctly means you're probably a good poster, look at the FAQ or the Tutorial Video for more information.