Problem with executing scripts
Hello,
Befor you start reading, I apologize for the length of this message. I tried to be as precise as possible on the steps I took, but since I'm a beginner, some of this might sound stupid...
So, I'm kinda new to unity and c#, and I'm practicing at programming, mostly by creating at least the start of a VG. What I do is that I randomly generate ennemies on a tileset, and 4 controllable players. Where the thing is getting tricky for me, is that I would like enemies and controlable characters to share the same set of combat variables, so to do this, I created a script for each enemy and character named "character_data.cs" with a unique namespace for each of these, and a single script named "combat_sheet.cs", that only contains the variables needed for combat. Both scripts are added to the characters and enemy prefabs, and in the start() of the script "character_data", for each variables, I do :
start(){ this.GameObject.GetComponent().variable_in_combat_sheet = variabe_in_character_data ; }
Since all my prefabs are generated using : Instantiate(prefab) as GameObject; I would expect all instantiated prefabs getting an independant copy of the "Combat_Sheet.cs" script. Obviously, this is not the behavior I observe :
I made a list of all the fighters on the tileset, that I sort following a variable called "next_turn", then I have 3 coroutines : -one that just wait for the space key to be pressed (to simulate player turn) -one that deals with turn for the enemies -A global one that alternates bewteen the other 2, depending on wether the first fighter in the list is a player or an enemy.
At the end of every turn, I do :
selected_fighter.GetComponent().next_turn += 25;
And then I sort the fighters again. It didn't seem to work, so at the sorting step, for all elements in the list, I do :
Debug.Log(fighter.GetComponent().next_turn);
So I always get to player's turn upon starting the game, where I'm expected to press Space, and when I do so, I see that the next_turn variables don't really work well : most of the time, all fighter's next_turn variable will change at the same time on pressing space, the values are different from what is shown on the scripts in the inspector window, and when I stop and restart the game, the values don't seem to have been reseted. Values are set back to 0 when reopening the project, although these are not supposed to be 0 (more like 20-25);
At some point, I tried removing the Combat_Sheet component from the prefabs and adding them at runtime, but then the sorting function would throw "Object reference not set to an instance of an object" exceptions.
I guess I'm doing something terribly and stupidly wrong, but I can't figure out what. Do you have any hint of what could be going wrong? Thanks KG
Answer by Kasumi_Gake · Nov 03, 2021 at 02:03 PM
Ok, I knew it was something stupid I found it : The function I use to instantiate the fighters returned the prefab and not the game object created... Sorry for the inconveniance, KG