Giving me an odd Error

Updated on December 8, 2018 in General
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on December 7, 2018

Here is my code, it keeps on giving me this error ” the name px does not exist in the current content. I tried changing it to a float and it still gave me the error

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckIfLaf : MonoBehaviour {
 public GameObject player;
 int lapsToGo = 3;
 bool Waypoint = false;
 // Use this for initialization
 void Start () {
 player = GameObject.FindGameObjectWithTag("Player");
 }
 // Update is called once per frame
 void Update () {
 float px = player.transform.position.x;
 float py = player.transform.position.y;
 float pz = player.transform.position.z;
 if (lapsToGo < 1)
 {
 // End the Game
 Destroy();
 }
 if (Waypoint == true)
 {
 // Check if past the Finish Line
 CheckIfFinished();
 }
 else
 {
 // Check if Player has crossed the Waypoint. This means no cheating
 // EXPLENATION: This means no cheating because i have put the waypoint exactly
 // at the half mark. So if he decides to go backwards after reaching the waypoint 
 // then he will still be covering the same distance
 CheckWaypoints();
 }
 }
 void CheckWaypoints()
 {
 // Waypoint Positions:
 // x: 81.1, y: 0, z: 30.1
// Checking x Pos for Waypoint
 if (px > 68 && px < 91)
 {
 // Checking z Pos for Waypoint
 if (pz > 15 && pz < 30)
 {
 Waypoint = true;
 }
 }
 }
 void CheckIfFinished()
 {
 // Checking x Pos for Finish Line
 if (px > -11 && px < 11)
 {
 // Checking z Pos for Finish Line
 if (pz > 58 && pz < 79)
 {
 lapsToGo -= 1;
 }
 }
 }
}
  • Liked by
Reply
4 on December 7, 2018

It says the variable px does not exist. It doesn’t. No where are you creating a variable called px (or py, or pz)

Add float in front of all of them to make them actual variables.

on December 7, 2018

K i did but it still gives me the arror along with the Destroy error along with the player error @Moledoux

Wise
on December 7, 2018

You didn’t mention any other errors. Is there an object with the tag “Player” in the scene? Destroy needs to know what you’re trying to destroy. I’ve never seen it called without any arguments.

 

Edit: I also now see that you use px in other functions besides Update. You need to either make them member variables, or copy paste what you have into all the functions where you want to use them.

on December 7, 2018

How do I make a variable to a member variable?

Wise
on December 7, 2018

Move it outside of the function, but keep them inside the class.

 

Show more replies
  • Liked by
Reply
Cancel
0 on December 8, 2018

Now it gives me this error: CheckIfLap.cs: A feild initializer cannot reference the nonstatic feild, method, or property

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckIfLaf : MonoBehaviour {
 public GameObject player;
 int lapsToGo = 3;
 bool Waypoint = false;
 // Use this for initialization
 void Start () {
 player = GameObject.FindGameObjectWithTag("Player");
 }
 float px = player.transform.position.x;
 float py = player.transform.position.y;
 float pz = player.transform.position.z;
 // Update is called once per frame
 void Update () {
 px = player.transform.position.x;
 py = player.transform.position.y;
 pz = player.transform.position.z;
 if (lapsToGo < 1)
 {
 // End the Game
 Destroy(gameObject);
 }
 if (Waypoint == true)
 {
 // Check if past the Finish Line
 CheckIfFinished();
 }
 else
 {
 // Check if Player has crossed the Waypoint. This means no cheating
 // EXPLENATION: This means no cheating because i have put the waypoint exactly
 // at the half mark. So if he decides to go backwards after reaching the waypoint 
 // then he will still be covering the same distance
 CheckWaypoints();
 }
 }
 void CheckWaypoints()
 {
 // Waypoint Positions:
 // x: 81.1, y: 0, z: 30.1
// Checking x Pos for Waypoint
 if (px > 68 && px < 91)
 {
 // Checking z Pos for Waypoint
 if (pz > 15 && pz < 30)
 {
 Waypoint = true;
 }
 }
 }
 void CheckIfFinished()
 {
 // Checking x Pos for Finish Line
 if (px > -11 && px < 11)
 {
 // Checking z Pos for Finish Line
 if (pz > 58 && pz < 79)
 {
 lapsToGo -= 1;
 }
 }
 }
}

Im sorry but im new to c#. That is why i have so many errors 

  • Liked by
Reply
Cancel