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 CubicPro · Aug 03, 2016 at 06:38 AM · c#scripting problemobjectspooling

Object Pooling

I am trying to make this code works, I have 6 prefab scenarios that I want to use in my game. in the start function I made 3 copies of each prefab, making a total of 18 pooled objects in the game.

when the function spawntile() is called I want that one of this 18 pooled objects that is inactive, turns active and show in the gameview. all other parts is working. just the part:

go = ActiveTiles [RandomPrefabIndex].SetActive (true);

go = ActiveTiles [PrefabIndex].SetActive (true);

dont work, I was using:

go = Instantiate (ActiveTiles[RandomPrefabIndex]) as GameObject;

go = Instantiate (ActiveTiles[PrefabIndex]) as GameObject;

and this works but create a new object, and I need the code active one of 18 inactive objects.

I am receiving this erros

error CS1502: The best overloaded method match for System.Collections.Generic.List<UnityEngine.GameObject>.this[int]' has some invalid arguments error CS1503: Argument #1' cannot convert method group' expression to type int' error CS0029: Cannot implicitly convert type void' to UnityEngine.GameObject'

thanks for the help!

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class TILEMANAGER : MonoBehaviour {

 public GameObject[] TileSPrefabs;

 private Transform PlayerTransform;
 private float SpawnZ = -10.0f;
 private float TileLenght = 10.0f;
 private float SafeZone = 15.0f;
 private int AmountTilesOnScreen = 8;
 private int LastPrefabIndex = 0;
 private int PooledTiles = 3;

 private List<GameObject> ActiveTiles;

 // Use this for initialization
 void Start () {
     ActiveTiles = new List<GameObject> ();
     PlayerTransform = GameObject.FindGameObjectWithTag ("Jogador").transform;

     foreach (GameObject objectprefab in TileSPrefabs){            
         for (int i = 0; i < PooledTiles; i++) {
             GameObject obj = Instantiate (objectprefab) as GameObject;
             obj.SetActive (false);
             ActiveTiles.Add (obj);            
         }
     }

     for (int i = 0; i < AmountTilesOnScreen; i++) {
         if (i < 3)
             SpawnTile (0);
         else
             SpawnTile ();
     }
 }

 // Update is called once per frame
 void Update () {
     if (PlayerTransform.position.z - SafeZone > (SpawnZ - AmountTilesOnScreen * TileLenght)) {
         SpawnTile ();
         DeleteTile ();
     }

 }

 void SpawnTile(int PrefabIndex = -1){
     GameObject go;
     if (PrefabIndex == -1)
         // the error is here//
         go = ActiveTiles [RandomPrefabIndex].SetActive (true);
     else
         // and here//
         go = ActiveTiles [PrefabIndex].SetActive (true);
     go.transform.SetParent (transform);
     go.transform.position = Vector3.forward * SpawnZ;
     SpawnZ += TileLenght;
     ActiveTiles.Add (go);
 }

 void DeleteTile(){
     ActiveTiles [0].SetActive (false);
 }

 private int RandomPrefabIndex(){
     if (ActiveTiles.Count <= 1)
         return 0;

     int RandomIndex = LastPrefabIndex;
     while (RandomIndex == LastPrefabIndex) {
         RandomIndex = Random.Range (0, ActiveTiles.Count);
     }

     LastPrefabIndex = RandomIndex;
     return RandomIndex;
 }

}

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 Landern · Aug 03, 2016 at 03:17 PM

You're calling a method line 47. You need to include the parenthesis.

Wrong:

 go = ActiveTiles [RandomPrefabIndex].SetActive (true);

Right:

 go = ActiveTiles [RandomPrefabIndex()].SetActive (true);
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

nut and bolt object construction system? 0 Answers

Distribute terrain in zones 3 Answers

Best way to defining scripts within a list inside the editor? 1 Answer

How can I get the Transform from an Object (target of UnityEvent)? 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