- Home /
How can I access a local variable in another scope?,I cant access the gameobject variable in the scope
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityStandardAssets.Characters.ThirdPerson;
public class Respawn : MonoBehaviour {
public GameObject playerPrefab;
public GameObject enemyPrefab;
public Vector3 playerOffset;
public Vector3 enemyOffset;
public bool playerDead = false;
public bool enemyDead = false;
void Start ()
{
}
void Update ()
{
if(playerDead == true)
{
GameObject playerRespawn = Instantiate(playerPrefab, playerOffset, Quaternion.identity);
playerDead = false;
}
if (enemyDead == true)
{
GameObject enemyRespawn = Instantiate(enemyPrefab, enemyOffset, Quaternion.identity);
enemyRespawn.GetComponent<AICharacterControl>().target = playerRespawn;
enemyDead = false;
}
}
}
How can I access a local variable in another scope? I want to automatically add the existing playerRespawn object to the enemy target.
This isn't possible. You have to declare your playerRespawn as a member variable so both 'if statements' can access it.
Answer by yayguy · Jan 03, 2019 at 04:50 AM
void Update ()
{
if(playerDead == true)
{
GameObject playerRespawn = Instantiate(playerPrefab, playerOffset, Quaternion.identity);
GameObject playerRes = GameObject.FindWithTag("Team Red");
GameObject enemyRes = GameObject.FindWithTag("Team Blue");
enemyRes.GetComponent<AICharacterControl>().target = playerRes.transform;
playerDead = false;
}
if (enemyDead == true)
{
GameObject enemyRespawn = Instantiate(enemyPrefab, enemyOffset, Quaternion.identity);
GameObject playerRes = GameObject.FindWithTag("Team Red");
enemyRespawn.GetComponent<AICharacterControl>().target = playerRes.transform;
enemyDead = false;
}
}
I can't believe I'm not stupid enough to set the prefabs with tag then find the Game Objects by tag, if there's any better way to do this please reply, thanks :)
Then you don't need GameObject playerRespawn = Instantiate(playerPrefab, playerOffset, Quaternion.identity);
. You can replace it with just Instantiate(playerPrefab, playerOffset, Quaternion.identity);
Answer by Dorscherl · Jan 03, 2019 at 03:07 AM
Local variables can only be accessed in the scope it's declared in. You can have a method that takes your GameObject as an argument so you can do with it as you need. Otherwise declare your object globally and set it as you need to.
How do I set my variable globally if I want it to be in playerDead? Or moreover, is there another way I can set playerRespawn globally with it being run after the player has died?
Answer by austinsun102 · Jan 03, 2019 at 02:54 AM
You can create a game object variable and then make the local variable equal to the game object variable you just created, or, another option is you can create a method that receives the game object as an argument.
That doesn't make sense. If he's going to create a member variable, then he doesn't need to create a local one.
Oops, sorry, I meant to say that you should set the variable you just created equal to the local variable.
Answer by Bran-A-Silva · Nov 27, 2021 at 05:18 AM
Late answer, but you can have a global GameObject in your script, then assign your local variable to be that object inside the function, then just refer to that GameObject in other functions:
GameObject localVariable
void Function1()
{
GameObject localBox = Instantiate(Box);
localVariable = localBox;
}
void Function2()
{
localVariable.gameObject.SetActive(false);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612184956im_/https://answers.unity.com/themes/thub/images/avi.jpg)