- Home /
Prefab not rendering when declaring Transform variable
Hi When making enemy AI, i ran into an issue that made my enemy's not render right. (my game is still in prototype phase)
This is the code i am using
public class enemy_behavior : MonoBehaviour
{
public float speed;
public Transform playerTransform;
Rigidbody2D rigidBody;
void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
rigidBody = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Vector3 direction = playerTransform.position - transform.position;
transform.rotation = Quaternion.LookRotation(direction);
rigidBody.AddForce(transform.forward * speed);
}
}
We don't know how your enemies are supposed to look like, we don't know if your enemies ever rendered correctly, and if so, what made them stop rendering correctly. $$anonymous$$ore info please.
@AconitumNapellum See my reply on my post for more detail
Answer by Squitz · Nov 06, 2019 at 09:06 PM
So for more detail. The thing that it makes it stop rendering properly is this line of code in script
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
I spawn them with this line of code
GameObject Spawnedenemy = Instantiate(enemy, new Vector3(transform.position.x + Random.Range(10, 50), transform.position.y - Random.Range(10, 50), 0), Quaternion.identity) as GameObject;
And this is what it schould look like:
Can you try commenting out your FixedUpdate() and running the game? I think there might be something up with your enemy's movement