- Home /
Why are floats not accessing my inputted values?
I'm trying to make my players location change upon entering a trigger. However, the "public floats" I've made don't seem to be working correctly. If I enter the collider, I get sent to (0, 0 ,0).
using UnityEngine; using System.Collections;
public class PlayerSwitch : MonoBehaviour {
public Camera myCamera;
public GameObject myPlayer;
public float one, two, three;
void Start ()
{
GameObject myPlayer = GetComponent<GameObject> ();
}
void OnTriggerStay (Collider other)
{
if (Input.GetKeyDown(KeyCode.F))
{
myPlayer.transform.position = new Vector3(one, two, three);
}
}
}
Comment
Answer by kornstar83 · Jun 08, 2017 at 06:07 PM
When I run this line of code in one of my scripts after declaring a public variable for it never actually Initializes the gameobject.
void Start ()
{
GameObject myPlayer = GetComponent<GameObject> ();
}
try initializing it like this and see what happens,
void Start ()
{
myPlayer = this.gameobject;
}
What I don't get is if this is your problem then why aren't you getting a nullReferenceException?
I tested the floats up above and it all worked fine for me once the myPlayer variable was properly assigned