2022-09-15

Despite having the same script as my prefab, the Instantiate() method clones aren't doing anything Unity2D. How do I fix this?

Hi I'm a bit new to Unity, making a top down game.

The prefab is an enemy I want to duplicate and it has a script to move around the player and stuff. I have all the fields initialized along with the script but the clone just doesn't do anything whilst the prefab itself does if dragged onto the grid.

Edit: I thought this was a lack of fundamentals and could be answered based off of an explanation. mb. i add screenshots.

Here's my copy of enemy: Rook(clone). This is the original prefab: Rook (they are the same)

This is the script for spawning in clones pasted below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gameplay : MonoBehaviour
{
    
    public GameObject originalRook;

    void Start()
    {
        spawnRook(3, 1);
    }

    // params = x coord and y coord to spawn rook
    void spawnRook(float x, float y) {
        Vector3 spawnLocation = new Vector3(originalRook.transform.position.x + x, originalRook.transform.position.y + y, 0);
        GameObject rook = Instantiate(originalRook, originalRook.transform);
        rook.transform.position = spawnLocation;

    }
}

Also, new edit: when I run game the gameObject script seems to run for like, 1 frame? like if i spawn the enemy in the aggro range it moves to my characters' tile once but then doesn't do anything after that. It acts once and never again (prefab always chases though they are the same).

I also forgot to mention an error I am getting: Cannot instantiate objects with a parent which is persistent. New object will be created without a parent.

This must be pretty critical but I don't really know how to work around it. I looked at some other posts but they seemed to have a different context. I know my error is in the script to initialize the clones but I don't know what I need to do to make it correct.



No comments:

Post a Comment