- Home /
The Name RespawnPlayer does not exist in the current context
So I've been working through this series of Unity tutorials by GamesPlusJames and he explains everything clearly, I've occasionally run into problems but usually it's been the result of a typo or I just need to make sure that I've entered in updated code for Unity 5.
For respawning mechanics however I've run into a very annoying problem and none of the usual tricks seem to work so I'm posting this up for you guys to look at, it's pretty self-explanatory code. All he's doing is creating a simple debug message so we can tell whether the player is colliding with an enemy or not.
I get the following error codes:
Assets/KillPlayer.cs(25,40): error CS0103: The name RespawnPlayer' does not exist in the current context Assets/KillPlayer.cs(23,17): error CS0029: Cannot implicitly convert type
string' to `bool'
KillPlayer Class
using UnityEngine;
using System.Collections;
public class KillPlayer : MonoBehaviour {
public LevelManager levelManager;
// Use this for initialization
void Start () {
levelManager = FindObjectOfType<LevelManager> ();
}
// Update is called once per frame
void Update () {
}
void onTriggerEnter2D (Collider2D other)
{
if (other.name = "Player")
{
levelManager = RespawnPlayer();
}
}
}
LevelManager Class
using UnityEngine;
using System.Collections;
public class LevelManager : MonoBehaviour {
public GameObject currentCheckpoint;
private PlayerController player;
// Use this for initialization
void Start () {
player = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void Update () {
}
public void RespawnPlayer()
{
Debug.Log ("Player Respawn");
}
}
Hope I've been clear enough with my post, it would be nice if I could have help looking at this conversion error as well but something tells me these problems are usually fixed with one line of code or even a letter rather than being a massive problem.
https://www.youtube.com/watch?v=ndYd4S7UkAU - here's the youtube video itself explaining everything
Answer by maccabbe · May 18, 2015 at 03:47 PM
If you want to use a method or variable of a class or object use Class.Method, so replace
levelManager=RespawnPlayer();
with
levelManager.RespawnPlayer();
Also, as a side note, Unity is case sensitive so onTriggerEnter2D should be OnTriggerEnter2D
Thank you! That immediately fixed the RespawnPlayer issue, as usual it was one lousy out of place character.
The only problem left to fix now is this convert error.
Just fixed it myself, I was using "=" ins$$anonymous$$d of "==" classic mistake because I didn't watch the video carefully enough, so I should be able to work through everything now.
level$$anonymous$$anager = RespawnPlayer();
should be
level$$anonymous$$anager == RespawnPlayer();
Thanks for all your help! That got me past a very annoying wall.
I'm worried about clogging up the board with posts so I'll keep this question in this one since it's all following a specific video. I have just one more line to worry about before everything is completely functional.
I'm getting the following error code:
Assets/Checkpoint.cs(25,38): error CS0120: An object reference is required to access non-static member `Level$$anonymous$$anager.currentCheckpoint'
Checkpoint Class
using UnityEngine;
using System.Collections;
public class Checkpoint : $$anonymous$$onoBehaviour {
public Level$$anonymous$$anager level$$anonymous$$anager;
// Use this for initialization
void Start () {
level$$anonymous$$anager = FindObjectOfType<Level$$anonymous$$anager> ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.name == "Player")
{
Level$$anonymous$$anager.currentCheckpoint = gameObject;
Debug.Log ("Activated Checkpoint" + transform.position);
}
}
}
This is for the very simple checkpoint class the guy demonstrates now I've already tried messing around and replacing "=" with "." and I've tried GameObject. Normally the types of error above just seem to be a complaint about na$$anonymous$$g conventions or the Level$$anonymous$$anager not having a target in the actual inspector window.
However after having checked all of that it seems like something else is going on.
Sorry, I just thought I'd mention for those who might think to answer this extra question I asked I ended up solving it. Unfortunately I've forgotten how! I'm pretty sure it was a simple case of just double checking my code though.