Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# : The name `' does not exist in the current context unity3d

Discussion in 'Scripting' started by itanium, Sep 8, 2012.

Thread Status:
Not open for further replies.
  1. itanium

    itanium

    Joined:
    Feb 23, 2012
    Posts:
    20
    Hello,
    I've searched a lot using google and forum searcher, but I didn't find answer for my problem.

    My problem :
    Why does this
    Code (csharp):
    1.    
    2. void Sart ()
    3.     {
    4.         GameObject monster = GameObject.Find ("monster");
    5.     }
    6. void Update ()
    7.         {
    8.     CharacterController controller = monster.GetComponent<CharacterController>();
    9.         }
    10.  
    or this code
    Code (csharp):
    1.    
    2. void Sart ()
    3.     {
    4.         CharacterController controller = GameObject.Find ("monster").GetComponent<CharacterController>();
    5.     }
    6.         void Update ()
    7.         {
    8.         guiText.text = "Speed : " + controller.velocity.y.ToString();
    9.     }
    10.  
    gives me a context related error? For the first code he can't find monster and for the second : controller.
    Why? I tried also using Awake() instead of Start(), but keep getting the same error.
     
    profitdefiant and LazyBear955 like this.
  2. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    Because it's telling you the truth. Those variables are not in the context that they're being called. The variable "monster" is defined inside the Start() method, so it can only be used inside the Start() method. Once Start() returns, the "monster" variable is thrown away, its memory is deleted. If you want "monster" to be available in the Update() method, then you need to define the variable inside the class context. In other words, you need to make it global, like this...

    Code (csharp):
    1.  
    2. class MyClass : MonoBehavior
    3. {
    4.    GameObject monster;
    5.  
    6.    void Start ()
    7.    {
    8.       monster = GameObject.Find ("monster");
    9.    }
    10.  
    11.    void Update ()
    12.    {
    13.       CharacterController controller = monster.GetComponent<CharacterController>();
    14.    }
    15. }
    16.  
     
    Last edited: Sep 9, 2012
  3. itanium

    itanium

    Joined:
    Feb 23, 2012
    Posts:
    20
    Thank you very much for your answer.
    It solved my problem and there's no need for a monster initialization in start(), just drag object in inspector to the script and everything is fine.
    Without dragging there was a error
     
  4. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    Sorry for missing that. That error pops up because the compiler doesn't know how Unity's engine works, or how any program works, for that matter. It doesn't know that Start() is guaranteed to be called before the Update() method, but it sees the possibility that Update() is called before "monster" is initialized.

    To avoid that error, you simply need to initialize the variable to something. In this case, you should set the variable to "null", because GameObject is a reference type. Alternatively, because this class inherits from MonoBehavior and you've attached it to a GameObject in the Unity editor, you can set the variable in the inspector. Unity generates the initialization code for the variable automatically prior to sending the program to Mono to be compiled.

    Code (csharp):
    1.  
    2.    GameObject monster = null;
    3.    .
    4.    .
    5.    .
    6.  
     
  5. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    your code
    Code (csharp):
    1. void Sart ()
    what I think you want

    Code (csharp):
    1. void Start()
     
  6. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    Minor spelling error. I've corrected it in my post. OP may or may not have copy/pasted that code directly from his project. In either case, it doesn't change the status of the original problem.
     
  7. kuteyash

    kuteyash

    Joined:
    Oct 22, 2020
    Posts:
    3
    I trying to fix error s but these four errors are coming :-The name 'target' does not exist in the current context but 2. The name 'screenPosition' does not exist in the current context 3.The name 'isMouseDragging' does not exist in the current context 4.The name 'offset' does not exist in the current context

    My code:-

    GameObject ReturnClickedObject(out RaycastHit hit)
    {
    GameObject targetObject = null;
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
    {
    targetObject = hit.collider.gameObject;
    }
    return targetObject;
    }

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    RaycastHit hitInfo;
    target = ReturnClickedObject(out hitInfo);
    if (target != null)
    {
    isMouseDragging = true;
    Debug.Log("our target position :" + target.transform.position);
    //Here we Convert world position to screen position.
    screenPosition = Camera.main.WorldToScreenPoint(target.transform.position);
    offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
    }
    }

    if (Input.GetMouseButtonUp(0))
    {
    isMouseDragging = false;
    }

    if (isMouseDragging)
    {
    //tracking mouse position.
    Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);

    //convert screen position to world position with offset changes.
    Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;

    //It will update target gameobject's current postion.
    target.transform.position = currentPosition;
    }

    }
    }
     
  8. AwesomeBuilder

    AwesomeBuilder

    Joined:
    Jan 19, 2021
    Posts:
    1
    Thank You So Much!
    I had issues with this because one of my global variables was giving me issues and I did not know why. it turned out that I initialized it in the class, gave it a value in Update() (variable = "get value from somewhere"), and then used the value in FixedUpdate.
    So I understand my error now. Thank you.
     
  9. ben0358632

    ben0358632

    Joined:
    Jan 13, 2022
    Posts:
    1
    Unity is saying the variable "loseTextObject" doesn't exist in the current context,

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class WallCollider : MonoBehaviour
    {
    public GameObject loseTextObject;

    // Start is called before the first frame update
    void Start()
    {
    loseTextObject.SetActive(false);
    }

    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag ("player"))
    {
    other.gameObject.SetActive (false);

    loseTextObject.SetActive(true);
    }
    }
    }

    but I defined the variable inside the class context.
    What do I do?
    Any help is appreciated!
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Please just create your own thread rather than hijacking an existing one. Post the code and the full error which also shows you the line/column numbers too.

    Also, when posting code, please use code-tags.
     
Thread Status:
Not open for further replies.