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 DubstepDragon · Feb 17, 2014 at 01:35 PM · spawncube

Random cube length on instantiate...

One of the GameObjects I am using in my game are cubes... But my game depends on cubes being of random lengths, almost like platforms, because my game is a FallDown clone where you have to keep going down or lose. I have a script that generates cubes and spawns them at random coordinates (included below). I also have a cube prefab, which is a generic cube to be spawned. I was thinking, maybe the way to do this is not through a prefab, but through another method I am not aware of.

When the cubes spawn, they move upwards due to directional gravity, giving the illusion of descending.

SCRIPT:

 using UnityEngine;
 using System.Collections;
 
 public class GOSpawn : MonoBehaviour {
 
     public float delay;
 
     public GameObject thingy;
 
     // Use this for initialization
     void Start () {
         InvokeRepeating("Spawn", delay, delay);
     }
     
     // Update is called once per frame
     void Spawn () {
         Instantiate(thingy, new Vector3(Random.Range(-6, 6), 10, 0), Quaternion.identity);
     }
 }

C# plezzz thanks (in advance) :D

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
2
Best Answer

Answer by clunk47 · Feb 17, 2014 at 07:11 PM

When you Instantiate, create a new instance of your prefab, or "clone" your "thingy" gameObject. Then create a Vector3 scale that caches the original scale of the clone. Next, change the scale variable on the desired axis or axes, then set your clone's localScale to the new scale setting.

 using UnityEngine;
 using System.Collections;
 
 public class GOSpawn : MonoBehaviour 
 {
     
     public float delay;
     public GameObject thingy;
     
     void Start () 
     {
         InvokeRepeating("Spawn", delay, delay);
     }
     
     void Spawn () 
     {
         GameObject thingy_clone = Instantiate(thingy, new Vector3(Random.Range(-6, 6), 10, 0), Quaternion.identity) as GameObject;
         Vector3 scale = thingy_clone.transform.localScale;
         scale.x = Random.Range (1, 4);
         thingy_clone.transform.localScale = scale;
     }
 }
 
Comment
Add comment · Show 6 · 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 clunk47 · Feb 17, 2014 at 07:30 PM 2
Share

I have to tag @Vexe in this because you made me say "Clone your thingy" lol.

avatar image vexe · Feb 17, 2014 at 08:22 PM 2
Share

lol you bastard

avatar image DubstepDragon · Feb 17, 2014 at 08:46 PM 1
Share

Jesus Christ :D

I'll see into cloning my "thingy" later... however thanks so much, I am always getting perfect help from you :D

avatar image clunk47 · Feb 17, 2014 at 08:49 PM 1
Share

Note I changed to a cast using "as" ins$$anonymous$$d of using a direct cast, Vexe kinda gave me some advice on that. http://stackoverflow.com/questions/132445/direct-casting-vs-as-operator

avatar image DubstepDragon · Feb 17, 2014 at 09:05 PM 1
Share

How relevant it is that we are talking about cloning "thingies", Vexe has 169 answers on his profile... 169... 69... shit.

Show more comments

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

20 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

Related Questions

A node in a childnode? 1 Answer

Spawing a SpawnPoint with Raycast 0 Answers

Random Spawning 1 Answer

Procedural Spawning Theorems 0 Answers

Respawn after falling off script? 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