- Home /
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 ();
}
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 .
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 ?
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
You are welcome! I am glad this solved your issue ! :) --What's the next issue ? I will be glad to help.
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
Answer by Trumble · Nov 05, 2012 at 01:51 AM
Here are a few things that come to mind:
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.
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.
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.
Your answer
Follow this Question
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