Instantiate not assigning new objects with changed variables?

So, I have a script that is on 2 GameObjects:

public int assignedRam; //variable declaration
    public PlayerController player;
    public int speed;

    void Start() {
        assignedRam = 0; //how much RAM to award the player (set for each individual enemy/player)?
        player = GameObject.Find ("player").GetComponent<PlayerController> (); //getting an instance of PlayerController from player
        speed = 5; //the speed at which the movement happens
        transform.position += transform.up * Random.Range(1f, 2f); //spawn the new object in a random location from the spawn, between 1 and 2 units
    }

    void Update() {
        transform.position = Vector3.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime); //set the position closer to the player's position at a rate of 'speed' every frame
    }

    void OnTriggerEnter2D (Collider2D col) {
        if (col.tag == "Player") { //is the object colliding the player?
            player.ramTotal += assignedRam; //if so, add a value to the RAM total
            Destroy(gameObject); //destroy the object
        }
    }

When “RAM” (the point system) touches the player, it assigns points. I leave the original objects at 0, and I change what it should be in each enemy script.

if(health <= 0) { //is the enemy health 0 or sub-0?
            StartCoroutine("DropRam"); //call the DropRam method
        }

        public IEnumerator DropRam() {
            ramZero.GetComponent<RamDrop>().assignedRam = ramToAssign;
            ramOne.GetComponent<RamDrop>().assignedRam = ramToAssign;
            yield return new WaitForSeconds(0.01f);
            for (int i = 0; i < 3; i++) { //execute the code inside 3 times
                Instantiate (ramZero, this.transform.position, this.transform.rotation); //make new copies of the RAM game object
                Instantiate (ramOne, this.transform.position, this.transform.rotation);
            }
            Destroy (gameObject); //if so, destroy the enemy
        }

And for each enemy, I set the assigned points equal to a private variable in each enemy script.

My problem lies in the new objects. The original objects values do change, but the new instantiated objects do not maintain the same settings. They return to 0. Can anyone tell me why? I’m kinda at a standstill here. If you know, let me know, thanks

Hi, I believe you are confusing instantiating new objects with cloning existing objects.

Prefabs are just object templates their values will always be set to their initial values or values given during their own instantiation (Start or Awake methods). These are distinct instances of the same object but do not share values. You will need to copy the values to the objects if you wish them to be the same or clone the original object instances.

Kind Regards,

Thanks for the response, and to clear up some confusion:
The objects I am instantiating are objects placed in the scene as templates and they appear as (Clone) in the inspector.
I am not calling resources from the Assets folder.
Would this change your answer anyhow? If so, how would I move the values over?

You are resetting assignedRam to 0 in Start.

2 Likes

Well that was easy enough, thanks man. I’m going to go hide in a corner now