Access non-static variable from static GameObject

I have been trying to figure out the past few hours how to access variables from a static GameObject. I have a script for defining a monster, and each pre-fab monster is defined using that script(just holds a good chunk of information unique to each). I want to be able to access that information for use in other scripts. I have tried using [ButtonText = MonsterOne.DefineMonster.Name] and [ButtonText = MonsterOne.Name], but those both give errors.

Can you post some code? It’s hard to make out what [ButtonText = MonsterOne.DefineMonster.Name] is trying to be in this context.

The Static Game object has a script attached to it with a good chunk of info pertaining to that specific monster, for example

//DefineMonster script held by static gameobject
public string Name = "Monster";

then inside another gameobject/script i have:

public GameObject Monster; //Static gameobject holding Name variable.unsure if needed
public UnityEngine.UI.Text NameHolder; //Text to be replaced with Monster's Name variable

...//skip the useless stuff like header and other vars

void Start{
   NameHolder = Monster.DefineMonster.Name //This does not work,and gives errors.unsure of working solution.
}

edit
is there a easier way, yet still stimple, of storing loads of lists(all be the same) of information than using a ton of gameobjects?(still have something easy to fill out, and hold lots of information, and being like the inspector panel with public dropdowns,scrollbars,strings…ect)

I’m not 100% sure what a ‘static GameObject’ is, but…

Remember your scripts are Components that are added to GameObjects.
I think above what you want is:
NameHolder = Monster.GetComponent().Name

I say static game object because I check the little box called “static” to the right of the name(in inspector)(seen in uploaded pic).

also, with “Monster.GetComponent().Name” , can I use a variable in place of “Monster”?
like, pass in a variable to replace the name of “Monster”(in my case, i define them by numbers such as “001”), so that I can grab any monster’s info I needed by changing a single variable.

2209965--146938--Screen Shot 2015-07-18 at 3.29.48 AM.png

Riiight. Been spending too long in C# land. Kind of forgot about that whole rendery/transformary/3D part of Unity. So these GO’s aren’t moving in 3D space and you’ve set them to static as a result.

In your code you defined Monster as a reference to a GO, so technically that can point to any object in your hierarchy if required. It sounds more like you need a Generic List or a regular array. Work out how you want to structure your data. It looks like you have a regular [seralizable] class that defines a monster. Now maybe you could have a Unity ‘script’ that’s has a public property of List, and this script is attached to your static GO. Now any time you have a reference to that GO, you have access to all your monsters.

The default inspectors can’t handle Generic lists, so you’d new to look at a assets store plugin to have Editor UI access to the data.

I am completely lost. My inspector currently looks like this(attached image. yes it says pokemon. using it to learn unity) and I like this format much; would a Editor Ui have the same abilities? or would I just be making an extremely long line of text defining everything and hoping it is in the right order and everything.

2210066--146942--Screen Shot 2015-07-18 at 6.28.39 AM.png

Ok, so that’s a type of monster in your game. You can create a prefab out of that GO and spawn it when you need it. But at some higher level in your code you’re going to need to keep a track of these spawned monsters so you can kill them. Usually whatever code spawned the prefab, should also update your list. The list is the variable that helps you find an individual monster at a later point.

How you implement that it totally up to you as a developer. Maybe you have a seperate GO with a separate script called MonsterManager or something.

These are not being spawned in. They are the base of each specific monster. Each monster on it’s own has a set of stats that are gone through the base stats- giving them their own unique stats.

I guess you want to do this:NameHolder = Monster.GetComponent<DefineMonster>().Name

Howver, if the DefineMonster is purely used to define a monster, but it will not be used on a spawned monster, then I think you are approaching it all wrong, and I would just make some data store class with a List containing all base monsterdefinitions. (List works in inspector if it is a field of a class).

I’ll see if I can get a working example for you to reference.

EDIT: Added a sample project to give an idea how I would go about defining monsters. The example I use allows you to easily change base information without making changes to instances of monsters as well as keeping the prefab/gameobject count low.

2210115–146956–Pokemon Battle Scene.rar (182 KB)

I looked over the project, and only confused about the instance part. At what part does the specific monster get chosen out of the list?(thanks alot for this. really helpful, and even learned a few new things about C#- like using {#} in string to make using variables easier)

In the Start() method of the MonsterManager class I call the SpawnMonster() method which handles the spawning of the monster, to choose one out of the list, depending on your use case you could do something like the following:

  • When walking in tall grass in the overworld there is a x% change of encountering a pokemon.
  • When triggering an encounter, the system will choose an id from a preset list of possible pokemon id’s for the area, with some kind of weight that defines how rare it is to encounter.
  • When an id is chosen SpawnMonser() is called and the id is used to spawn the monster.

The same can be used when fighting another trainer, you can just give a trainer a fixed array of pokemon id’s and then call SpawnMonster to spawn the chosen pokemon during a fight.

Since it hasn’t explicitly been mention, you generally don’t want to tag a monster with static in the inspector. Static in the inspector means it is designed to never be moved in game. Unity does stuff to optimise it, knowing it will always be in the same position. Moving it will cause performance issues.

Incidentally this has absolutely nothing to do with the static key word in C#