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 Lordinarius · Nov 04, 2012 at 07:01 PM · destroycloneoptimise

How could i optimize this code

I am tring to make game like "Same" but my code is too slow. Its jamming my pc and phone ram usage rising to 700 mb when it stucked.

here is my code. In code, cubess are seeking other cubes that is tagged same. And if there is same cube left side or right side of cube, it is destroying it self and creating another block

 function Start () {
 
 }
 
 var sonraki: GameObject;
  function FindMassObj () 
     {
     
     var gos : GameObject[];
     gos = GameObject.FindGameObjectsWithTag("c3"); 
     
     
     
     
     
     for (var go : GameObject in gos)
 
   { 
    if (go.transform.position.y==transform.position.y) {     
   if (go.transform.position.x-transform.position.x==1 || go.transform.position.x-transform.position.x==-1 ) {   
   
        Destroy (gameObject);
          
             
             var clone: GameObject;
             clone = Instantiate(sonraki, transform.position, transform.rotation) as GameObject;
   
          }
          }
     } 
     
 }
 function  Update () {
 
 FindMassObj ();
 
 
 }
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
Best Answer

Answer by Montraydavis · Nov 04, 2012 at 07:28 PM

You could use Raycasts ! :D Much more efficient.

 function Update () {
     var leftRay = transform.TransformDirection (Vector3.left);
     var rightRay = transform.TransformDirection (Vector3.right);
     if (Physics.Raycast (transform.position, leftRay, 10)) {
         print ("There is something on the right of the object!");
     } 
 
     if (Physics.Raycast (transform.position, rightRay, 10)) {
         print ("There is something on the left of the object!");
     } 
 
 }

If you need to access the colliders, check out Physics.Raycast

Just add your if & instantiate codes where they belong.

You will notice two things:

1) A lot less code

2) You're not having to use CPU to check against every single object. Only the ones who are on the left/right :D

I hope this solves your issue .

Good luck, and thanks for using Unity .

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 Lordinarius · Nov 04, 2012 at 07:50 PM 0
Share

Thakns, it looks use full. But i should get left or right side objcts idendity or tag to destroy my object

for example

6-7-6

my cube is seven, and seven seeking same value as itself, if left or right side object is seven they will destroy themself

is it possible this with raycast ?

avatar image Montraydavis · Nov 04, 2012 at 07:58 PM 0
Share

Yes. That's why I said to look at Physics.Raycast if you wanted to get the collider.

Here you go:

 var frontHit : RaycastHit ;
 var front : boolean = Physics.Raycast(Vector3(transform.position.x, transform.position.y, transform.position.z), transform.TransformDirection( Vector3.forward ), frontHit, 15);
 
 //Access collider ---> frontHit.collider.tag ;

Please mark as answered if this works for you. Thanks :D

avatar image Lordinarius · Nov 04, 2012 at 08:46 PM 0
Share

Thanks mate. Problem solved. Now next problem :D

avatar image Montraydavis · Nov 04, 2012 at 08:49 PM 0
Share

You are welcome! I am glad this solved your issue ! :) --What's the next issue ? I will be glad to help.

avatar image Lordinarius · Nov 04, 2012 at 09:36 PM 0
Share

http://dl.dropbox.com/u/69926746/WebPlayer.html

Normally in game, if same cubes are side by side. they should reduce together untill 1. But as you can see its working buggy mostly, blocks are missing to control left or right object untill destroyed itself. it is destroying itself before the check.

Edit: Oh i solved it :)

it was buggy when if (Physics.Raycast(transform.position, leftRay,hit,1))||(Physics.Raycast(transform.position, leftRay,hit,1))

i wrote statments separately and it solved :) thanks again

Show more comments
avatar image
0

Answer by Trumble · Nov 05, 2012 at 01:51 AM

Here are a few things that come to mind:

  1. You could limit the calls to `FindMassObj()`. If it doesn't necessarily need to be called every frame, maybe limit it to every 0.25 seconds or so.

  2. Retrieving go.transform.position is more than a simple reference to a variable. It's backed up by a C# property, and performs some hefty matrix math to calculate the position every single time you request it. Try caching it using `Vector3 currentPosition = go.transform.position;` and use `currentPosition` wherever it's needed.

  3. Cloning and destroying objects is pretty computationally expensive, and becomes a big problem if you're doing a lot of it. If it applies to your situation, you can use an object 'pooling system.' Instead of instantiating and destroying objects at runtime, you can create a pool of objects. Pull an object from the pool and activate it when you need it, then deactivate it and put it back in the pool when you're done with it.

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 Lordinarius · Nov 05, 2012 at 07:42 AM 0
Share

Thanks for these usefull informations. :)

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

13 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

Related Questions

Destroy instance clone of gameobject 2 Answers

Why could I delete my clones onlY in the same order of instantiation? 2 Answers

How do I properly destroy an instance of a prefab from within its own script? 1 Answer

Destroy a specific instantiated clone? 2 Answers

How to destroy a gameobject's clone when colliding with the ground? 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