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 /
  • Help Room /
avatar image
0
Question by opnsrc · Jan 06, 2017 at 09:30 PM · c#gameobjectprefabprefabsactivate

How do you set Prefabs into a GameObject array that will activate/deactivate when called?

Hi everyone, got a question - hope someone can help. I stored 3 different prefabs into my GameObject. On Start() I set them to be false. Then I randomly turn one on at a time, in a Coroutine also found in the Start(). However, when I try to run the game, nothing shows up on the screen. Any help would be must appreciated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class GameManager : MonoBehaviour {
     // When initializing GameObjects make sure path to prefabs are set properly
     public GameObject[] moles = new GameObject[3];
         
     // Use this for initialization
     void Start () {
 
         //moles = Resources.LoadAll("Enemy") as GameObject[];
 
         foreach (GameObject i in moles)
         {
             i.SetActive(false);
         }
 
         StartCoroutine(Gameloop());
     }
 
     IEnumerator Gameloop()
     {
         yield return new WaitForSeconds(1f);
         yield return StartCoroutine(RandomMoleSpawn());
         yield return StartCoroutine(EndSequence());
 
     }
 
     //Mole generator...
     private IEnumerator RandomMoleSpawn()
     {
         int loop = 0;
 
         //My infinite loop...
         while (loop < 1)
         {
             int Index = Random.Range(0, moles.Length);
             moles[0].SetActive(OnOff());
             yield return new WaitForSeconds(0.5f);
         }
     }
 
     //The ON and OFF switch for my moles...
     public bool OnOff()
     {
         int Index = Random.Range(0, 3);
         if (Index > 0)
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 
     private IEnumerator EndSequence()
     {
         Reset();
         Debug.Log("Finished!");
         yield return new WaitForSeconds(1f);
     }
 
     //Returns everything back to original state...
     public void Reset()
     {
         foreach (GameObject i in moles)
         {
             i.SetActive(false);
         }
     }
 
     // Update is called once per frame
     void Update () {
         
     }
 }
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
Best Answer

Answer by KoenigX3 · Jan 06, 2017 at 10:35 PM

If you have assigned prefabs to it, you will see no change on the screen. You have to assign gameobjects from the scene to the 'moles' array, or instantiate them at runtime, and assign them to the array.

Right now you are changing the active state of the prefab, and not the objects on the screen (if you have placed any objects into the scene).

(Note: you can actually see the prefab's state in the Project window as the code executes, as it changes the first prefab)

EDIT: I've added a code which instantiates the 3 prefabs with 5 units distance on the X dimension.

 public GameObject[] prefabMoles = new GameObject[3];
 GameObject[] moles = new GameObject[3];
 
 void Start () {
 
     for(int i = 0; i < prefabMoles.Length; i++)
     {
         moles[i] = (GameObject) GameObject.Instantiate(prefabMoles[i], new Vector3((i-1)*5, 0, 0), Quaternion.identity);
     }
 
 }

Note that i have changed the name of the prefab array to 'prefabMoles', and the array of the spawned gameobjects is named 'moles' - which means that the rest of the code will modify the 'moles', which are spawned on the scene.

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 opnsrc · Jan 07, 2017 at 03:16 AM 0
Share

Thank you 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

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

Character Selection 0 Answers

[HELP C#] Making Gameobject 1 equal Prefab 2 1 Answer

How to add a prefab menu to inspector in a Gameobject? 0 Answers

How to assign GameObject to a instantiated prefab via Script(C#)? 2 Answers

Why does Unity lock the transform of prefabs in my scene in playmode? 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