• 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 Jrobson · Jul 01, 2019 at 07:46 PM · prefab-instance

SetActive = false on previous instantiated prefab

I am using the following code to create a line of prefabs

 {
  
  public GameObject myPrefab;
  
  Vector3 lastPosition = new Vector3(0,2,0);
  Vector3 offsetVector = new Vector3(0,0,-3);
 
  void Update()
  {
      if(Input.GetKeyDown (KeyCode.Space))
      {
      GameObject myPrefabInstance = GameObject.Instantiate(myPrefab, lastPosition + offsetVector, Quaternion.identity);
      lastPosition = myPrefabInstance.transform.position;
      }
  }
 }

This works great and gives me a nice even row of prefabs. However I want the player to be able to adjust the position of each prefab once it is placed and have an arrow key move script attached to the parent prefab. Of course this means that currently all the prefabs move in unison. How do I disable the previous prefab (perhaps with SetActive = false) once the space bar is pressed and the next prefab is placed. Currently I only want the player to be able to move each prefab once.

Thanks for any suggestions.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by metalted · Jul 01, 2019 at 09:08 PM

You could save the myPrefabInstance as a global GameObject. Each time the Space bar is pressed down, the variable will be overwritten to the newly created gameobject. You then use your movement script on that saved gameobject. When the space bar is pressed again, the new gameobject is assigned to the variable and can be controlled using your script.

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
avatar image
0

Answer by Jrobson · Jul 02, 2019 at 04:37 PM

@metalted OK, bear with me, I'm a relative noob. I get what you're saying in principle but haven't been able to find an example of this by searching "save prefab instance as global GameObject". Is there another way to phrase this that will help me find some pointers? 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 metalted · Jul 02, 2019 at 08:59 PM 0
Share

@Jrobson Ofcourse! So if I understand correctly, you want to create a prefab, let the player control that prefab and then create another prefab, control that one etc... Right now you are creating a GameObject myPrefabInstance inside the if(..$$anonymous$$eyDown..) statement. This means that the so called "scope" of this GameObject can not go outside of the if statement. It is also not available in the rest of the class, like in the other functions. This is what called a "local variable". Local variables are declared inside a function and are only used inside that function.


Then there are the global variables. They are declared inside the class but OUTSIDE of a function. This means that the variable is available to all the functions inside the script. It can even be available outside the script. You can use the "Access level modifiers" for that. You probably seen the public and private keywords. Public variables are accessible from any script, but only if you have a reference to that script.

 Wallet wallet = new Wallet();
 Debug.Log(wallet.money);
 //the money variable has to be declared public for this to be possible


The private variables can only be accessed from within the script. This is used for protection so people can't alter them directly. This is sometimes combined with a function to get and set the variable. This is called "Encapsulation". It not only makes sure the variable gets changed the right way, it also gives user a nice way to interact with your script.


Then there are protected variables, those can only be access by the script they are in AND the script that inherit from that class. "Inheritance" is also something very interesting to look at.


Static variables are a whole different things, but very straightforward its a variable accessible from any script anywhere without a reference to it.


Okay so that enough information for a "relative noob!" Lets get back to the question. If you save the GameObject myPrefabInstance outside of your function, so a global variable like GameObject myPrefab, you can access it later to apply movement to it. Right now, when the key is pressed the gameobject is declared inside the if statement and the next frame it is gone. So when you "Save the prefab instance as global gameobject", it means to assign the newly created gameobject ( a copy from your prefab ) , to the global variable ( a GameObject variable that is inside your class but outside of any function ). After you assign it you could pass it into a function to move it, or apply movement directly by using gameObject.transform.Translate() (or something). I hope this gives you an idea.

avatar image metalted · Jul 02, 2019 at 09:02 PM 0
Share

If you want to start coding quickly, I suggest going through the book "Headfirst C#" or "Headfirst Java". Its both Object Oriented Program$$anonymous$$g. It $$anonymous$$ches basic stuff like variables and types and modifiers to advanced topics like interfaces, polymorphism, inheritance etc etc. I really had fun reading it and doing it, cause there are a lot of exercises and puzzles in there too!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 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

Related Questions

Why can't I undo changes to a prefab? (or to an instance of it after I apply the changes) 1 Answer

Unity 2D Game Issue: Script is only affecting one prefab instead of all of them. 0 Answers

Client unable to retain reference to prefab 0 Answers

How to instantiate prefabs exactly in the way as drag'n'drop does? 1 Answer

Adding a child via code in edit mode won't work for prefab instance 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