1
votes

I am trying to load a prefab from a file and create a clone of the loaded file without instantiating it. Changing the data values of the loaded clones script, should then not change the prefabs values.

I am in a way trying to create a clone of a cloned and loaded prefab.

Loading a prefab into unity using Resources.Load:

 enemy = new Player();
 enemy.playerColor = Color.red;
 enemy.Name = "enemy";
 enemy.unitList.AddResources.Load("Prefab/Soldier", typeof(GameObject)) as GameObject);

Loading a prefab with the instantiate clone function:

enemy = new Player();
enemy.playerColor = Color.red;
enemy.Name = "enemy";
enemy.unitList.Add(Instantiate(Resources.Load("Prefab/Soldier", typeof(GameObject)) as GameObject) as GameObject);

Changing the loaded prefab:

public void updateUnit(string prop, float val)
{
    switch (prop)
    {
        case "Movespeed":
            Movespeed += (int)val;
            break;
        case "SpawnRate":
            SpawnRate += val;
            break;
    }
}

The current problem is that using the instantiate function on the prefab, creates the clone on the screen. Whilst using the loaded prefab results in changes in the script in the prefab

1
Have you thought of having your prefab inactive? That way when you instantiate it, it will not appear on screen. Another solution is to position the instantiated prefab outside of the camera view. You can call Camera.main.ViewportToWorldPoint() and give it an x or y value below 0 or above 1 to position it outside the camera's viewkUr4m4
Yeah that would be a rough workaround. I have also considered having the "hard" coded value of the prefab reset by the startup script.Illuminate

1 Answers

-1
votes

1: create a gameobject and assign the prefab to it any way you want

private GameObject toclone;

2:create a gameobject to clone onto, so you can call it later

private GameObject clone;

3:run this command to summon it. replace Position with a vector3 and Rotation with a quaternion (if you want to use vector3 for rotation, use Quaternion.Euler)

clone = Instantiate(toclone, Position, Rotation, Quaternion.Identity);