Question by
chris56728 · May 08, 2016 at 06:33 AM ·
c#uicanvas
C# - The name does not exist in the current context
Hello World!
I'm having an error:
The name `tqgameController2' does not exist in the current context
Not sure what i'm doing wrong in my code to get this error. Any suggestions?
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections;
using TriviaQuizGame;
using TriviaQuizGame.Types;
public class OnClick_Button_Canvas : MonoBehaviour
{
public Button buttonPrefab; //default button
void start()
{
GameObject go = GameObject.Find("GameController");
TQGGameController tqgameController = (TQGGameController)go.GetComponent(typeof(TQGGameController));
TQGGameController tqgameController2 = new TQGGameController();
buttonPrefab.onClick.AddListener(delegate { MyMethod(0); });
}
void MyMethod(int index)
{
Debug.Log("Button Clicked");
tqgameController2.ChooseAnswer(index);
}
}
Comment
Best Answer
Answer by TBruce · May 08, 2016 at 06:44 AM
tqgameController2 is local to the Start() function only. It needs to be global to the whole class like this
public class OnClick_Button_Canvas : MonoBehaviour
{
public Button buttonPrefab; //default button
private TQGGameController tqgameController;
private TQGGameController tqgameController2;
void start()
{
GameObject go = GameObject.Find("GameController");
tqgameController = (TQGGameController)go.GetComponent(typeof(TQGGameController));
tqgameController2 = new TQGGameController();
buttonPrefab.onClick.AddListener(delegate { MyMethod(0); });
}
void MyMethod(int index)
{
Debug.Log("Button Clicked");
tqgameController2.ChooseAnswer(index);
}
}