• 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 BinarySmash · Oct 26, 2012 at 03:23 PM · javascriptinstantiateprefabscore

How can I instantiate more prefabs as the score increases?

Hi Everybody

I was wondering if someone can help me with this problem please. I'm trying to increase the amount of my prefabs instantiated in my game depending on the score that the player has. Here's the script attached to my game manager object:

 var INCREMENTpoints : int = 0;
 var SCOREdisplay : String = "The score is: ";
 
 function Points() {
 
     INCREMENTpoints++;
     if(INCREMENTpoints == 5) {
     
          dropAsteroids.AMOUNTgenerated++;
     
     }
 
 }
 
 function OnGUI() {
 
     INCREMENTpoints.ToString();
     var dialRect : Rect = Rect ((Screen.width/2)-200,(Screen.height/2)-200, 400, 400);
     GUI.Box(dialRect, SCOREdisplay + INCREMENTpoints);
 
 }

Here's the script that is attached to my main camera:

 var asteroid : GameObject;
 var gameOver: GameObject;
 static var AMOUNTgenerated : float = 1.0;
 
 var isONLYcreatedONCE: boolean;
 
 private var leftRange : Vector3 = new Vector3(-5.139884, 3.581706, -6.911701);
 private var rightRange : Vector3 = new Vector3(-5.139884, 3.581706, 7.066976);
 
 function Start ()
 {
 
     InvokeRepeating("createAsteroid", 2, AMOUNTgenerated);
 
 }
 
 function createAsteroid ()
 {
     var randomZ = Random.Range(leftRange.z, rightRange.z);
     
     var asteroidLocation : Vector3 = new Vector3(leftRange.x, leftRange.y, randomZ);
     
     Instantiate(asteroid, asteroidLocation, Quaternion.identity);
 }
 
 function Update()
 {
 
  if(HealthBar.health <= 0 && !isONLYcreatedONCE)
  {
  
     HealthBar.health = 0;
     Debug.Log(isONLYcreatedONCE);
     
     
     Instantiate(gameOver, gameOver.transform.position, gameOver.transform.rotation);
     
     isONLYcreatedONCE = true;
     CancelInvoke(); 
     } 
     
 }

Thank you for your help.

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 NewEonOrchestra · Oct 26, 2012 at 04:30 PM

Why not create a for loop that spawns new asteroids? Say, if the player score is 50,000, divide that by 1000 to spawn 50 asteroids.

for (int i=0; i < PlayerScore / 1000; i++) { //spawn asteriod }

Obviously only call this method on rare occasion, perhaps if the player clears out a level, or maybe on a delay timer so the next wave pops in every 60 seconds... whatever your game calls for

Comment
Add comment · Show 4 · 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 BinarySmash · Oct 26, 2012 at 08:26 PM 0
Share

I don't think that is ideal because my current code works fine. If you read my code samples properly you would realize that I don't really need a for loop. I just need something that could update the A$$anonymous$$OUNTgenerated variable in the start function.

avatar image NewEonOrchestra · Oct 26, 2012 at 09:56 PM 0
Share

The for loop would make them all spawn at once, not what you want I suppose. But the formula I wrote above is pretty much the same.

Why don't you update A$$anonymous$$OUNTgenerated using player score as a numerator over some constant as a deno$$anonymous$$ator, like I write in my psuedocode snippit above?

A$$anonymous$$OUNTgenerated = (some constant)*(player's score)

That makes it a linear function in the form of: f(x) = mx + b .

The higher player score, the more asteroids are called to spawn in your InvokeRepeating method every 2 seconds, as you have it.

Or you could use a quadratic function, the way it takes more XP to get to the next level than the last, present in most RPGs.

avatar image BinarySmash · Nov 08, 2012 at 06:56 PM 0
Share

What do you mean by "Some constant"?

avatar image NewEonOrchestra · Nov 08, 2012 at 11:04 PM 0
Share

By "some constant" I mean a mathematical constant. Dividing x over 45 means that the result is only 1 after x reaches 45.

I'm not entire sure of what you are trying to do, but I think you may not be using InvokeRepeating correctly. The parameters are like this:


function InvokeRepeating (methodName : String, time : float, repeatRate : float) : void Description

Invokes the method methodName in time seconds.

After the first invocation repeats calling that function every repeatRate seconds.


So if you are making A$$anonymous$$OUNTgenerated larger, you are calling the createAsteroid function less frequently, not more.

What I think you want to do is this:

[code] dropAsteroids.A$$anonymous$$OUNTgenerated = INCRE$$anonymous$$ENTpoints / 30; [/code]

This means A$$anonymous$$OUNTgenerated will increase by one every 30 points. But InvokeRepeating is taking A$$anonymous$$OUNTgenerated as a delay between asteroid invocations, so try this ins$$anonymous$$d:

[code] dropAsteroids.A$$anonymous$$OUNTgenerated = 1 / (INCRE$$anonymous$$ENTpoints / 30); [/code]

By dividing 1 over INCRE$$anonymous$$ENTpoints, you are making it so that the higher score you get, the lower A$$anonymous$$OUNTgenerated becomes, which means the more frequently createAsteroid will be called. (At this point you may want to rename A$$anonymous$$OUNTgenerated to asteroidSpawnFrequency or something.

Also, move that equation into an the update method ins$$anonymous$$d of start so the values are updated during gameplay.

Now: I don't know if InvokeRepeating will always look at the current value of A$$anonymous$$OUNTgenerated or the value you passed in when you first made the call at Start. I will try and test this out later.

In the mean time, do you understand the math in all this? It's basic algebra so I'm sure you probably do.

avatar image
0

Answer by edrocks4u · Oct 26, 2012 at 10:16 PM

Just change how you increase your score from score++; to a method and call the method instead.

 public void IncreaseScore(int i) {
 //up your score
 score += i;
 
 //instaniate your prefab
 Instantiate(gameOver, gameOver.transform.position, gameOver.transform.rotation);
 
 }
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.

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

11 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

Related Questions

Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer

How to instantiate on collision? 0 Answers

After instantiating an instance of a prefab and changing the value of a variable in a script attached to the prefab, that value is lost 1 Answer

Cannot assign prefab in Inspector? 2 Answers

I need one of my prefabs to be deleted after a few seconds 2 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