1
votes

I have a script that moves instantiated game objects depending on their tag. However, the game objects that have been instantiated are not moving.


Instantiation Script:

void Update()

{
    tillNextSpawn += Time.deltaTime;
    Debug.Log(tillNextSpawn);

    if (tillNextSpawn >= 2)
    {
        UnityEngine.Debug.Log("Instantiating circle");
        screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));
        Instantiate(circle, screenPosition, Quaternion.identity);
        tillNextSpawn = 0.0f;
    }
}

Enemy controller script(moves the enemies)

void FixedUpdate()
{
    /*GameObject[]*/
    var objects = GameObject.FindGameObjectsWithTag("Enemy");
    var objectCount = objects.Length;
    foreach (var obj in objects)
    {
        // Move the players accordingly
        var rb = obj.GetComponent<Rigidbody2D>();
        Debug.Log(rb);
        Vector2 direction = (player.position - obj.transform.position).normalized;
        rb.velocity = direction * moveSpeed;
    }


}
2

2 Answers

1
votes

If you want the enemy to follow the player, try doing the following:

//Attach this script to the enemy

Transform player;
private Rigidbody2D rb;
private Vector2 movement;
public float moveSpeed;

    void Awake()
{
    player = ScriptNameOnPlayer.instance.gameObject.transform;

}


// Start is called before the first frame update
void Start()
{
    rb = this.GetComponent<Rigidbody2D>();
}

void Update()
{

    Vector3 direction = player.position - transform.position;
    direction.Normalize();
    movement = direction;  
}

void FixedUpdate()
{
    moveCharacter(movement);
}

void moveCharacter(Vector2 direction)
{
    rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
}


//But make sure your player script has this line of code:

public static ScriptNameOnPlayer instance;

Hope this helps !

1
votes

You should be able to that, and the issue maybe somewhere else in your project. I created a sample project that recreates what you're trying to do, trying to be as similar as I could be to the sample code you send, you can find it here:

https://github.com/webocs/unity-so-sample-tags

For what I can see, your console is sending an exception

get_main is not allowed to be called...

What comes to my mind, is that that exception is breaking the entire execution, and that's why nothing is happening.

As a side note, I don't know your project so I don't really know why you're building it this way. Said that, why aren't you creating an Enemy script that's attached to the enemy prefab? If you have many enemies you're going to be finding and iterating through all of them in each update tic. If you create an Enemy script and attach it to the prefab you should be able to handle the movement of the enemy using the transform of the gameObject that the script is attached to. This way each enemy is a standalone entity.

I hope all of this helps!

Edit: I've edited the repo and added a scene called "IndividualEnemies" that illustrates what I told you in the comments