The name `followerPosition' does not exist in the current context
I am attempting to get a game object to follow another game object and keep getting this error i am not sure what this error means.
using UnityEngine;
using System.Collections;
public class Follower_Movement : Player
{
public bool aFollower = true;
public GameObject player = GameObject.Find("Player");
Vector3 behindPlayer = new Vector3(0, 0, -1);
// Use this for initialization
void Start()
{
Transform playerTransform = player.transform;
Vector3 followerPosition = playerTransform.position;
}
// Update is called once per frame
void Update ()
{
if (aFollower == true)
{
followerPosition = followerPosition - behindPlayer;
}
}
}
Answer by Brocccoli · Mar 18, 2016 at 05:24 PM
Move your followerPosition declaration to the top of your Object
public class Follower_Movement : Player
{
Vector3 followerPosition;
...
}
Then set it in your Start method
void Start()
{
Transform playerTransform = player.transform;
followerPosition = playerTransform.position;
}
Then you can use it in your Update method.
A word to the wise. If you only set the followerPosition at the Start, than you will not have an updated position as the player moves, so I would suggest not only setting it at the start, but also setting it in your update method so you always have the correct location.
Thank you for the help that did work however now it gives me these errors:
Find can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function.
UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Ins$$anonymous$$d move initialization to the Awake or Start function. Follower_$$anonymous$$ovement..ctor ()
You should make another question to cover that. Tag me in the new question and I can help you out.
Also, please upvote the answer if it helped you, thanks.