Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Nicolinux · Jan 21, 2012 at 01:23 AM · instantiateprefabupdatemessage

Keep constant number of prefabs

This should be fairly trivial and I don't know what's wrong with my script. I seem to have trouble understanding how to send messages.My gamemanager script keeps track of the number of prefabs and has two functions that are called when a prefab is created/destroyed. I checked that both CountPlatformsUp() and CountPlatformsDown() are called, but my "platforms" variable doesn't keep track and once destroyed platforms are not spawned in Update().

Why?

From the prefab:

 function Start () {
     gameManagerObject.GetComponent(_gameManagerScript).CountPlatformsUp();
 }

 function OnDisable() {
     gameManagerObject.GetComponent(_gameManagerScript).CountPlatformsDown();
 }


The gamemanager:

 var platformPrefab : GameObject;
 var spawnAreaObject : GameObject;

 private var spawnAreaTransform;
 private var platforms : int = 0;

 function Start () {
     spawnAreaTransform  = spawnAreaObject.GetComponent(Transform);
 }

 function Update () {
     if (platforms <= 20) {
         SpawnPlatform();
     }
 }

 function CountPlatformsUp () {
     platforms = platforms + 1;    
 }

 function CountPlatformsDown () {
     platforms = platforms - 1;
 }

 function SpawnPlatform() {
     var targetPos : Vector3 = Vector3(Random.value * spawnAreaTransform.position.x, Random.value * spawnAreaTransform.position.y, 1.434096);
     Instantiate(platformPrefab, targetPos, Quaternion.identity);
     //platforms = platforms + 1;
 }
Comment
Add comment · Show 2
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 Bunny83 · Jan 21, 2012 at 05:31 AM 2
Share

aldonaletto already gives you a better solution for your problem, but your original way have some problems:

  • If you use OnDisable, you should use OnEnable as counterpart.

  • it seems your platform script have a public variable called "game$$anonymous$$anagerObject" . Do you assign the gamemanager object somewhere? Esp. for new created platforms? Note that you can't assign objects from the scene to variables of prefabs.

  • The game$$anonymous$$anagerObject variable should be of type "_game$$anonymous$$anagerScript" so you avoid the GetComponent calls.

  • A better way would be to make the Game$$anonymous$$anager a singleton by holding a public static reference to the manager object.

In the "_game$$anonymous$$anagerScript.js" script:

 static var instance : _game$$anonymous$$anagerScript;
 function Awake()
 {
     instance = this;
 }

In your platform script:

 function OnEnable () {
     _game$$anonymous$$anagerScript.instance.CountPlatformsUp();
 }
 
 function OnDisable () {
     _game$$anonymous$$anagerScript.instance.CountPlatformsDown();
 }
avatar image Nicolinux · Jan 21, 2012 at 10:17 AM 0
Share

$$anonymous$$any thanks for the suggestions. To my defense - I am coding a quick prototype, thus the messy coding. Should have mentioned it. I'd like to understand why the "if (platforms <= 20)" does not get called in Update(). Any ideas?

  • I was using OnDisable because Unity lacks OnDestroy(). But you are right, OnEnable would be better especially if I were to reuse objects ins$$anonymous$$d of instantiating new ones.

  • Both the gamemanager and the platforms are prefabs and I assigned a reference from the gamemanager to the platform prefab.

  • You are right, thanks.

  • Good idea with the singleton. I am aware of the pattern but still new to Unity.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Jan 21, 2012 at 02:37 AM

Is the game manager script called "_gamemanagerScript.js"? You should pass the script name without quotes or extension to GetComponent - if the name is "GameManagerScript.js", use GetComponent(GetManagerScript).

Anyway, there's a good method to keep track of the current population of platforms: create an empty object, call it "Platforms", reset its position and rotation and assign the script below to it. Whenever a new platform is created, it's childed to the Platforms object - this way you can just check childCount to know how many platforms are alive (Unity always updates the childCount when a child is added or destroyed).

var platformPrefab : GameObject; var spawnAreaObject : GameObject;

private var spawnAreaTransform;

function Start () { spawnAreaTransform = spawnAreaObject.transform; // get transform directly }

function Update () { if (transform.childCount

function SpawnPlatform() { var targetPos : Vector3 = Vector3(Random.value spawnAreaTransform.position.x, Random.value spawnAreaTransform.position.y, 1.434096); var platf: GameObject = Instantiate(platformPrefab, targetPos, Quaternion.identity); platf.transform.parent = transform; // child the new platform to this object }

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 Nicolinux · Jan 21, 2012 at 10:19 AM 0
Share

Very cool. Thanks. Will do that. I wonder though why my initial and messy script wasn't working correctly.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I Instantiate a prefab to touch and still update a score? 1 Answer

Prefabs Transforms LookAt 0 Answers

My randomly generated slope game doesn't seem to work? 0 Answers

Instantiate prefab within parameters?(Javascript) 1 Answer

Destorying prefab and instanstiating again? 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