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 LightSource · Sep 05, 2013 at 10:24 PM · javascriptgenerating

Generating with scripts turned off?

Hey all,I have an odd situation. I have this script attached to a prefab that is spawned at the start and moves towards the player. When it hits the player, it raycasts and spawns another one of itself, or of a similar object.

My problem is, when it generates, half(?) the cubes behave as they should, but the other half starts with the generation and movement script off by default. I don't even reference the script below. So eventually, the cubes just stop coming because they are all bunched up in one area.

I hate to pose long scripts, so if this is a bit too intimidating for a casual forum, please feel free to ask me to take it off. I will gladly do so. Thanks a bunch!

 var visable : boolean = false;
 
 var Cube : Transform;
 var platformCube : Transform;
 var Empty : Transform;
 var emptyCube : Transform;
 
 var chances : int;
 var generate : boolean = false;
 
 private var startSize : int = 25;
 private var newPos : int = 0;
  
 //********************************
 //Raycast
 //********************************
  
 function Update () 
 {
 
     //Move
     transform.Translate(Vector3.left * movementSpeed, Space.World);
     
     var up = transform.TransformDirection(Vector3.up);
     var hit : RaycastHit;  
  
     if (Physics.Raycast (transform.position, up, hit)) 
     {
        generate = true;
        Destroy(gameObject);
        generateEmpty();
     }
  
     //********************************
     //Generate chances
     //********************************  
  
     chances = Random.Range (1,101);
  
     if (generate == true) 
     {
         generate = false;
         
        if (chances <= 60) 
        {
          generateFloor();
        }
  
        if (chances <= 40) 
        {
          generateDrop();
        }
  
        if (chances <= 0) 
        {
          generatePlatform();
        }
     }
 }
  
 //********************************
 //Generate level
 //********************************  
  
 function generateFloor () 
 {
     var newCube = Instantiate (Cube, transform.position + Vector3(startSize, 0, 0), Quaternion.identity);
     newCube = Instantiate (Cube, transform.position + Vector3(startSize + 1, 0, 0), Quaternion.identity);
 }
  
 function generatePlatform() 
 {
     var newplatformCube = Instantiate (platformCube, transform.position + Vector3(startSize, 3, 0), Quaternion.identity);
 }
  
 function generateDrop() 
 {
     var newCube = Instantiate (Empty, transform.position + Vector3(startSize, 0, 0), Quaternion.identity);
 }
 
 function generateEmpty() 
 {
     var newEmptyCube = Instantiate (emptyCube, transform.position + Vector3(newPos, 0, 0), Quaternion.identity);
 }
  
  
 //*********************************//
 function OnBecamevisible () 
 {
     visable = true;
 }
 
 function OnBecameInvisible () 
 {
     Destroy(gameObject);
 
 }
     
Comment
Add comment · Show 11
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 -hiTo- · Sep 06, 2013 at 02:05 PM 1
Share
 Destroy(gameObject);
 generateEmpty();

This seems pretty o$$anonymous$$ous. You are disposing of the gameobject before you call a function from it... Any particular reason for doing this?

avatar image fherbst · Sep 06, 2013 at 02:12 PM 0
Share

I seoond that... at least switch the order.

avatar image Jamora · Sep 06, 2013 at 08:27 PM 1
Share

There's something off in your generation chances too. Namely that you have ~40% chance to not spawn anything and 0% to spawn a Platform. I dunno if this is connected to your issue.

avatar image OP_toss · Sep 06, 2013 at 09:20 PM 1
Share

Also Destroying an object automatically when it becomes invisible is sketchy... Don't auto-destroy things based on visibility. Those are 2 different things that you probably want to explicitly set.

Also 'visable' is spelt 'visible'.

Is your prefab by chance affected? If you look at the prefab when you run this, do the scripts disable? If so, you may be calling Destroy on the prefab somehow... I would name your variables a little nicer so you know what's what.

Are all the scripts on the instantiated object being turned off? Or just specific ones?

avatar image ArkaneX · Sep 06, 2013 at 11:00 PM 1
Share

You're actually generating emptyCube at the same position after raycast hit (newPos is never changed, at least in above script). This new cube automatically raycast, hit, spawn another empty object + generates additional cubes based on random value and so on.

Show more comments

2 Replies

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

Answer by -hiTo- · Sep 06, 2013 at 09:26 PM

Still seeing a pretty ominous situation with that Destroy...

You want to keep running the function after calling destroy on that script. Which is weird. I would call Destroy at the end, only if generate is true.

Another thing: your percentages are off(?), like Jamora said.

But it's more than what Jamora led on.

You are checking each and every if-statement.

Which means that if your value is 30, both if-statement #1 and if-statement #2 will run, and you will instantiate both a floor and a drop. You are also randomizing between 1 and 100, so your value can never be 0. Your last statement, the platform, will never run.

Here's what I mean you should do instead:

 function Update ()
 { 
     //Move
     transform.Translate(Vector3.left * movementSpeed, Space.World);
  
     var up = transform.TransformDirection(Vector3.up);
     var hit : RaycastHit;
  
     if (Physics.Raycast (transform.position, up, hit))
     {
         generate = true;
         generateEmpty();
     }
  
     //********************************
     //Generate chances
     //********************************
 
     if (generate == true)
     {
         chances = Random.Range (0,100); // No need to call a Random if we're not going to check the value. So move it here.
  
         if (chances >= 60) // 40 percent chance
         {
             generateFloor();
         }
         else if (chances >= 40) // 20 percent chance
         {
             generateDrop();
         }
         else // 100% - 40% - 20% = 40 percent chance
         {
             generatePlatform();
         }
         
         Destroy(gameObject); // It is unnecessary to set generate to false in here. 
                              //Because it will not run again, since we are destroying the object that this script runs on.
     }
 }
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
avatar image
2

Answer by ArkaneX · Sep 06, 2013 at 11:08 PM

I'm glad that solution given by @-hiTo- works for you. I did a bit of research as well, and just for those wishing to learn a little more about similar problem and its cause, please take a look at this question.

When I was testing code provided by you, I've seen similar problem - script was disabled and names were longer and longer (Cube, then (Clone), then Cube(Clone)(Clone), etc.). After testing solution provided in linked question by @Bunny83, everything started to work as expected.

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

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

How can I generate random number except one in javascript 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Generate a random number that is different every time? 1 Answer

How to let my gun shoot ?? 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