Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by tshellberg · Sep 28, 2017 at 10:59 AM · objectsinstantiate prefabinstantiationscopecontext

Unable to access instantiated GameObject within else statement

I'm building a simple spaceship 2D shooting game and built a reflector prefab which reflects the incoming missiles/lasers back at the enemies. The reflector works great, and I added some code to instantiate the shield GameObject when the 'S' button is pressed. The instantiation itself works fine, but I'm not able to find a way to smoothly destroy it after the 'S' button is pressed again. The purpose is to be able to "toggle" the Reflector on and off.

However, I'm not able to access the newly instantiated GameObject outside of the if() statement. The answer is probably stupidly easy but I can't figure out why it's not accessible. If I copy the Destroy() statement to within the if() statement, it's accessible, but I don't know why it's not outside of that.

Any help would be greatly appreciated.

alt text

Here's the code:

 public class playership : MonoBehaviour {
 private bool reflectorActive = false; //initialize the reflector to false
 
 void Update () {
 
 if (Input.GetKeyDown (KeyCode.S)) {
             reflectorActive = !reflectorActive; // toggle the boolean
             if (reflectorActive) {
                          //instantiate a new Reflector GameObject using prefab.
                 GameObject newReflector = Instantiate (Reflector, transform.position, Quaternion.identity) as GameObject;
                 newReflector.transform.SetParent (this.transform);
             } else {
                                 // destroy the reflector after it's toggled off
                 Destroy (newReflector);
             }
         }
 }
 }


screen-shot-2017-09-28-at-125307-pm.png (77.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by WitchLake · Sep 28, 2017 at 11:39 AM

Try to save the gameobject as private value of your class:

 public class playership : MonoBehaviour {
    private bool reflectorActive = false; //initialize the reflector to false
    private GameObject reflector;
 
    void Update () { 
       if (Input.GetKeyDown (KeyCode.S)) {
          reflectorActive = !reflectorActive; // toggle the boolean

          if (reflectorActive) {
             //instantiate a new Reflector GameObject using prefab.
             GameObject newReflector = Instantiate (Reflector, transform.position, Quaternion.identity) as GameObject;

             //saves the newly created reflector
             reflector = newReflector;

             newReflector.transform.SetParent (this.transform);
          } else {
             // destroy the reflector after it's toggled off
             Destroy (reflector);
          }
       }
    }
 }



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 tshellberg · Sep 28, 2017 at 02:41 PM 0
Share

That is now working, thanks! I'm not sure I understand why though, so it would be great if someone could explain it. I'm guessing that instantiating the newReflector GameObject doesn't automatically make it accessible within this class? If so, where is it with regards to Scope?

If it's a problem of scope, is there anything that can be added to the Instantiate line that automatically scopes it properly given the current class?

GameObject newReflector = Instantiate (Reflector, transform.position, Quaternion.identity) as GameObject;

Thanks for the help!

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

115 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

Related Questions

Infinite Instantiation 2 Answers

Invisible Prefab spawning at location of Player before being instantiated 1 Answer

Change value of a static variable multiple times in one script 2 Answers

Why do my first 50 instances of a prefab have the same properties? 1 Answer

How to Change the Variables of an Instantiated Object? 0 Answers


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