- Home /
How to change "Smooth Follow" script value in another script
I'm trying to make it to when the character hits an object, the script's value in the camera, smooth follow, will change. Here's what I have so far: var maincamera : GameObject; var cameratrigger : GameObject;
function OnControllerColliderHit( hit : ControllerColliderHit)
{
if (hit.gameObject.transform.name == "Cameratrigger")
{
maincamera.GetComponent("Smooth Follow").Height = 1.5;
}
}
I'm getting the error "NullReferenceException: Object reference not set to an instance of an object". What's wrong here.
Answer by Julien-Lynge · Sep 20, 2011 at 09:18 PM
Two questions off the bat:
What line does the error refer to? (You can see the line listed at the end of the error dialog)
Have you actually defined maincamera and cameratrigger in the inspector, and made sure that maincamera has a component named "Smooth Follow"?
Those questions being asked, you have to look at a script itself, not the script name in the inspector, to see what the script is called. In your case you're probably looking for SmoothFollow, not Smooth Follow, since scripts typically don't have spaces in their names.
The "maincamera.GetComponent("Smooth Follow").Height = 1.5;" line. I actually changed it to "maincamera.GetComponent("SmoothFollow").Height = 1.5;". After that it brang up an error saying height doesn't exist. What's wrong there?
Answer by Design3.com · Sep 21, 2011 at 01:57 AM
The error saying Height doesn't exist probably means that the variable is misnamed and is actually all-lowercase in the SmoothFollow script. Just change "Height" to "height". One mechanic for naming variables is to always start with a lowercase and use capitals for new words (i.e. mainCamera, height, myNewVariable). Functions, in contrast, start with a capital (i.e. Update, GetComponent).
Your answer
Follow this Question
Related Questions
Changing target of SmoothFollow camera 2 Answers
SmoothFollow script issue for multiplayer car game 0 Answers
why target switching of smooth camera not very smooth ? 0 Answers
Mouse Orbit to SmoothFollow and back (Euler to Quaternion possible?) 2 Answers
Specific Smooth camera follow tutorial 0 Answers