• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yayguy · Jan 03, 2019 at 01:42 AM · instantiategetcomponent

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sean244 · Jan 03, 2019 at 02:49 AM 0
Share

This isn't possible. You have to declare your playerRespawn as a member variable so both 'if statements' can access it.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sean244 · Jan 03, 2019 at 04:53 AM 1
Share

Then you don't need GameObject playerRespawn = Instantiate(playerPrefab, playerOffset, Quaternion.identity);. You can replace it with just Instantiate(playerPrefab, playerOffset, Quaternion.identity);

avatar image yayguy sean244 · Jan 03, 2019 at 05:03 AM 0
Share

Thanks, lol.

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yayguy · Jan 03, 2019 at 04:28 AM 0
Share

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?

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sean244 · Jan 03, 2019 at 02:56 AM 0
Share

That doesn't make sense. If he's going to create a member variable, then he doesn't need to create a local one.

avatar image austinsun102 · Jan 03, 2019 at 05:17 AM 0
Share

Oops, sorry, I meant to say that you should set the variable you just created equal to the local variable.

avatar image
0

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);
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting a component of an instantiated object returns an error 1 Answer

[solved]how can i enable or disable a component of instantiated objects? 5 Answers

Assign a Side to a spawned Unit in a two sided Towers game 0 Answers

Instantiate Prefabs. Errors. 2 Answers

Play iTween on instantiated prefab 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges