Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by adichav · Apr 08, 2016 at 12:22 PM · prefabsresources

Controlling prefabs from Resources Folder

I have loaded a bunch of prefabs which have objects on the scene. Now I choose to randomly disable one from the script. As the object on the scene is attached to the prefab and the prefab is controlled by the script, I assumed I could setActive (false). This didn't work. Is there a way to disable the prefab set in the resources such that it reflects on the scene.

 public GameObject[] tableObjects;
 void Start () 
     {
         tableObjects = Resources.LoadAll<GameObject> ("GameObjects");
                 for (int i=0; i<objCount; i++) 
         {
             tableObjects[i].SetActive (true);
         }
        }
 
     void SelectRandomObjects(int GameLevel) {
         for (int i = 1; i <= GameLevel; i++) {
             int x = Random.Range(0,30);
             if (tableObjects[x].activeSelf == false)
                 i--;
             else
             {
                 tableObjects[x].SetActive (false);      
             /* This disables the prefab but only when I load the scene again does it appeared disabled */

} } }

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
0

Answer by browne11 · Apr 08, 2016 at 02:18 PM

@adichav Perhaps you can Destroy (gameObject); then instantiate the object back when needed unless you're trying to store info in that object.

Or try to enable/disable the script or component with this line of code. gameobject.GetComponent< SomeScript >().enabled = true;

Let me know if that helps.

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 adichav · Apr 13, 2016 at 02:19 PM 0
Share

@Cover Club $$anonymous$$edia

Ill explain the game slightly so you may get a better idea. I am disappearing objects statically placed in the scene. The number of objects disappearing is based on the level. There are toggles for the user to use, so that they may be able reappear the object and should they find all the objects they beat the level. Now I am trying to dynamically obtain all the objects and create a toggle for them, so that if I add a new object to the scene and add its prefab to the resources folder, I should be able to use it.

Couldn't use either,

The Destroy(gameObject) was not friendly as there are close to 30 objects to disappear and reappear, each with different locations. During some levels, as many as 10 objects disappear and they have to reappear upon clicking on the options. Furthermore, destroying the object would mean that I couldn't check again the options available to user.

I tried disabling the mesh of the game object, but what happens is that only after the game stops (ending Play-mode) does the mesh component turn off.

Hope the info helps you to help me :D

avatar image browne11 adichav · Apr 14, 2016 at 03:47 AM 0
Share

@adichav The mesh route shouldn't have that desired effect but it doesn't sound like the solution to your problem.

Did you try object pooling? You have something similar. If you have a return statement that would return the false status properly. Sounds like that would be what you need. In the below video he uses it for bullets which will add up also. http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

  public class ObjectPoolScript : $$anonymous$$onoBehaviour
  {
      public GameObject pooledObject;
      public int pooledAmount = 20;
      public bool willGrow = true;
      
      public List<GameObject> pooledObjects;
      
      void Start ()
      {
          pooledObjects = new List<GameObject>();
          for(int i = 0; i < pooledAmount; i++)
          {
              GameObject obj = (GameObject)Instantiate(pooledObject);
              obj.SetActive(false);
              pooledObjects.Add(obj);
          }
      }
  
      public GameObject GetPooledObject()
      {
          for(int i = 0; i< pooledObjects.Count; i++)
          {
              if(pooledObjects[i] == null)
              {
                  GameObject obj = (GameObject)Instantiate(pooledObject);
                  obj.SetActive(false);
                  pooledObjects[i] = obj;
                  return pooledObjects[i];
              }
              if(!pooledObjects[i].activeInHierarchy)
              {
                  return pooledObjects[i];
              }    
          }
          
          if (willGrow)
          {
              GameObject obj = (GameObject)Instantiate(pooledObject);
              pooledObjects.Add(obj);
              return obj;
          }
          
          return null;
      }
      
  }
 

Hopefully that helps

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

About to gut my use of the Resources folder. What are the best practices going forward? 1 Answer

Nested prefab in Addressable prefab 0 Answers

Resources.loadall not loading prefabs at runtime. 1 Answer

how Load Prefabs after build 0 Answers

In-Game Resource Management (How to?) 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