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