# Simplify this code

GameObject a = new GameObject();
GameObject b = new GameObject();
a.transform.position = transform.position;
b.transform.parent = a.transform;
b.transform.localPosition = Vector3.zero;
b.transform.localRotation = Quaternion.identity*Quaternion.Euler(180, 90, 90);
a.transform.LookAt(target.transform);
transform.rotation = b.transform.rotation;
Destroy(a);
Destroy(b);
The purpose of this code is to rotate a transform to point at an object. I want it to be as if the parent of the transform looked at the object, and the child had a local rotation of (180, 90, 90). This code does what I want, however, it creates and destroys two game objects every frame, which I’m certain is unnecessary. So, I would greatly appreciate if anyone could tell me how to rewrite this code so that it doesn’t create any game objects.

One trick when working with quaternions is to just think of them as rotations - when multiplying by a quaternion (from right to left), you are simply performing a rotation.

``````//Find the rotation from our transform in the direction of the target
Quaternion rot = Quaternion.LookRotation (target.transform.position - transform.position);
//Set the rotation to (180,90,90) and then rotate by the look rotation
transform.rotation = rot * Quaternion.Euler (180, 90, 90);
``````

without creating gameobjects at runtime? I mean you would just have the two cubes already created and reference them?
_
public GameObject cube1;
public GameObject cube2;
_
If you’re problem is running the code inside Update you could do something like this: (You could either do something to the cubes in Update after the gameobjects are created, or invoke a method that does something)
_
public class SomeClass : Monobehaviour
{
private bool hasCreatedGameObjects;

``````    void Update()
{
if (!hasCreatedGameObjects)
{
hasCreatedGameObjects = true;

GameObject a = new GameObject();
GameObject b = new GameObject();

DoSomethingWithCubes(); // call a method that does something to the cubes
}

// or do something with cubes per frame
}
}
``````