- Home /
Do game objects "inherit" the classes of their script components
Ok, so here it is. I'm not sure I've grasped what the "script components" really do. I've created my own class that all "mobs" (monsters, players, you name it) should belong to. I want to visualize the mobs with a, let's say, 3D object. So I create the object and attatch the script containing the script with public/serialized variables that I can change for each individual 3D object, right?
Below is the code for my class. But how would I pass the 3D object into a function like "void Greet(MobInfo mobInfo)"?
To sum it up: 1) Are the variables I assign in the inspector tab SPECIFIC for that individual 3D object? (or do I have to add some code saying "MobInfo player = gameObject" with an individual subclass for each 3D object)
2)If 1) is true, how would I carry on and retrieve the 3D object as a member of the MobInfo class.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MobInfo : MonoBehaviour {
public bool isPlayer;
[SerializeField]
public Dictionary<string, int> status = new Dictionary<string, int>() //MAIN STATS --- NOTE!!! These should only be changed when recieving a PERMANENT bonus
{
{"HP", 50},
{"MP", 50},
{"STR", 10},
{"DEX", 10},
{"MAGIC", 10},
{"EVASION", 1},
{"DEFENSE", 10},
{"SPEED", 1}
};
public string currentMove;
[SerializeField]
public Spell currentSpell;
public bool isTurn;
private string[] moves = {"Attack", "Defend", "Spell", "Run"};
private bool showGUI;
public Dictionary<string, int> ModStats = new Dictionary<string, int>(); //Used to store equipment bonus or (de)buff effects (temporary changes)
public Dictionary<string, int> currBuffs = new Dictionary<string, int>(); //Will reflect the remaining duration of a (de)buff
public void ChooseMove() {
if (isPlayer) { //If it's the player, show the GUI
showGUI=true;
currentMove = ChoiceGUI ();
} else { //If it's the computer, make a random choice
int rand = Random.Range(0,2);
currentMove = moves.GetValue (rand).ToString();
}
}
public string ChoiceGUI() {
if (showGUI) {
GUI.Box (new Rect (10, 10, 100, 140), "");
if (GUI.Button (new Rect (20, 40, 80, 20), "Attack")) { //MAKE ATTACK BUTTON
showGUI = false;
return "Attack";
}
if (GUI.Button (new Rect (20, 70, 80, 20), "Defend")) { //MAKE DEFEND BUTTON
showGUI = false;
return "Defend";
}
if (GUI.Button (new Rect (20, 100, 80, 20), "Spell")) { //MAKE SPELL BUTTON
showGUI = false;
return "Spell";
}
if (GUI.Button (new Rect (20, 130, 80, 20), "Run")) { //MAKE RUN BUTTON
showGUI = false;
return "Run";
}else {
return "Error";
}
} else {
return "Error";
}
}
}
you're right - you haven't really grasped how they work ;)
variables are specific to an instance of the script component attached to a GameObject
unless they're marked as static
.
you add this script component to each object and to access it's variables use GetComponent
to get a reference to that script and its variables.
you might consider using a class/struct ins$$anonymous$$d of a Dictionary
too, unless there's a specific reason you chose it...
Thanks! :) I realize how they work now (I hope) and for the first time really understand what static "really means" :P
So basically, all gameobjects with the script "$$anonymous$$obInfo" will have it's individual variation (instance) of that script run and I should be able to pass on these variables as "$$anonymous$$obInfo NPC = gameObject.GetComponent();" and then shove NPC into a function, e.g. void Greet($$anonymous$$obInfo mob)? :) The function Greet(NPC); would then access variables from the instance of the class $$anonymous$$obInfo that is associated with the gameObject, right?
Your answer
![](https://koobas.hobune.stream/wayback/20220613172347im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to deal with GameObject class and my custom inheritance 1 Answer
Adding script during runtime 2 Answers
Prefab variants inherited scripts 1 Answer
Lines of code playing multiple times? 1 Answer
How unity works under the hood? 0 Answers