XCode, error: '_object' undeclared. Need some help solving this problem

I've got this code in my viewController.m file

- (void)viewDidLoad {
    [super viewDidLoad];
    GameLogic *_game = [[GameLogic alloc] init];
    [_game initGame];

    .......
}

GameLogic is another class which I created. in the same viewController.m file, I have got another function

- (void)test {
    if([_game returnElecFence]) //[_game returnsElecFence] causes the error
    {
        NSLog(@"YES");
    }
    else {
        NSLog(@"NO");
    }
.......
}

Problem is, whenever the test function is called, I get an error saying '_game' undeclared. I tried putting the GameLogic init code in the .h file and on top of the @implementation to make it global but every method I tried resulted in a worse error. TIA to anyone who can suggest some ideas to clear this error up

Answers


_game is a local variable. Its scope is only the method in which it's declared (viewDidLoad in this case).

You need to make _game a global variable, or better yet, an instance variable of your viewController class so that it can be accessed by all methods of the class.


Need Your Help

How to get a program to continue reading input after if statements are fulfilled (Morse Translator)

java if-statement morse-code

Before I begin, I am a novice programmer having only been doing this for about a day.