Hello !
I’m currently working on a personnal project and have an issue i can’t seem to find a solution to. I’ve looked on the forum and can’t find a similar problem.
(I’ll link the scripts below)
In my project i generate 16 rooms(custom classes) that have different values(id name description etc…)
and they all have one single enemy of type Hostile(another custom class).
When i generate the rooms, i create a random room from a json, and then adds to it the values that i want and a random hostile(from an hostiles json too), then i send this room to my manager which holds all the rooms(a rooms[ ] array), and i do this 16 times for all the rooms;
My issue is this one:
Later on, if i am in a room with my character, and attacks the enemy of that room, all the enemies that have the same name are affected.
ex: 10 rooms have a ghost enemy, 6 have a monster enemy. I attack a ghost in my room, all the ghosts in the 6 rooms which have ghosts take damage.
I hope i’m clear on my issue :/.
It’s like all the enemies of the same name are connected, even if i specifically created a new Hostile for each room.
Here’s the code related to that issue:
The Room class
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Room
{
public int id;
public int objectLocation;
public string name;
public string description;
public string iconPath;
public int roomLevel;
public Hostile enemy;
public Item[] enemyLoot;
public bool hostileRoom;
public Item[] loot;
}
The Hostile class
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Hostile
{
public int id;
public string name;
public string description;
public int health;
public int attack;
public int defense;
public int prob;
public int[] specialLootID;
}
The part when i generate the rooms
for(int i = 0; i < 16; ++i){
encounterManager.GenerateRoom(i);
}
The actual function generating the rooms
public void GenerateRoom(int i){
Room room = new Room();
room = mapManager.GetRandomRoom();//Returns a random room
if(room.hostileRoom){
room.enemy = GetHostile(room.roomLevel);//Returns a random hostile
room.enemyLoot = GetEnemyLoot(room.enemy);
}
room.loot = GetLoot();
Manager.instance.rooms[i] = room;
}
The hostile creation ( GetHostile() )
public Hostile GetHostile(int roomLevel){
Hostile hostile = new Hostile();
hostile = hostiles[Random.Range(0,hostiles.Length)];
while(Random.Range(0,100) > hostile.prob){
hostile = hostiles[Random.Range(0,hostiles.Length)];
}
return hostile;
}
And the moment where i actually change the value
public void Weapon1Hit(){
int damage = Random.Range(Manager.instance.character.weapon1.powerMin,Manager.instance.character.weapon1.powerMax);
Manager.instance.rooms[Manager.instance.currentRoomID].enemy.health -= damage;
mainViewUI.WriteInLog("You hit " + Manager.instance.rooms[Manager.instance.currentRoomID].enemy.name + " for "
+ damage + " damage with " + Manager.instance.character.weapon1.name);
//Attack();
}
Don’t hesitate if you want more information or other pieces of code where the problem could be coming from.
I use a singleton, that’s why i reference like : Manager.instance. , so i can keep the data between scenes.(for that issue i never change scenes so the problem can’t come from this).
Thanks
Guilhaume