- Home /
Retrieving Script component from gameobject.
As of right now i'm processing a lot of game-objects with different variations of one base class. BaseClass, myClass1 : BaseClass, etc...
i have a "List<'BaseClass> myList", that i put the derived classes into.
I serialized the data in my list and i'm bringing it back into game, i couldnt save the list as a whole directly because of the whole "inherits from Mono" error i would get when i did my binary-format. so the individual attributes are stored off and need to be reassigned.
So when the player loads their save, i create a new list of BaseClasses, and because i can't seem to find a better alternative I created a ResourseName property in my BaseClass so my derived classes could assign the prefab name to retrieve the original prefab if needed. i load up a game resource based on the obj the original derived script was assigned to.
GameObject go = Resources.Load("ResourseName") as GameObject;
now its a child of this Object that has the script i want assigned to it, i attempt to grab this as so:
Transform _child = go.transform.FindChild("_child");
BaseClass _childClass = _child.GetComponent<BaseClass>();
It would be nice if i could get a list of the scripts attached to the _child so i could find out EXACTLY which derived class i was dealing with. But i cant get that to work either. i tried:
MonoBehavior[] mb = _child.gameobject.GetComponents<MonoBehavior>();
but it never returned the childs components, just kepts returning itself, when i was expecting results like: "Transform", "DerivedClass". The GameObjects actual attached components.
I have an "public override void setup()" in my derived classes that holds its specific data, and on Start() in the BaseClass that setup is ran. Passing all the Derived variables to the BaseClass.
Now the whole reason i went about creating the GameObject go in the 1st place was so on creation it would recreate a new instance of its given DerivedClass, that i could use as a blank sheet to reasign all the de-serialized data. HOWEVER, all the derived variables are blank and missing creating a new GameObject from my pre-setup prefab dosen't appear to be working as i intended. Any Thoughts? Thanks!
void OffLoadStats()
{
//This is the deserialized data
ps_ = sl.RetrievePS_();
//new list used to hold reconstituted components
List<Soul> mySI = new List<Soul>();
//how many diffrent soul objs player had.
int soulCount = ps_.si_.mySouls_.Count;
for(int i = 0; i < soulCount; i++)
{
//grabs the resoursename to load the resourse
string RN = ps_.si_.mySouls_[i].ResourseName;
go = Resources.Load("creatures/"+RN) as GameObject;
if (go == null)
Debug.Log("Didn't load any resources");
else
{
//this is a child of the loaded resourse
Transform _soul = go.transform.FindChild("_soul");
//SoulSwap simply returns a copy of the derived class attached to _soul
Soul goS = _soul.GetComponent<Soul>().SoulSwap();
//adjust new soul to loaded souls level
goS.addXP(ps_.si_.mySouls_[i].);
mySI.Add(goS);
}
//*grabs the instance of the
players stats sheet and replaces the default blank list
with a reconstituted on*//
foreach (Soul s in mySI)
ps.si.AddSoul(s);
}
Or am i just completely WRONG!?
Your answer
Follow this Question
Related Questions
When to Destroy Multiple Items 0 Answers
how to set custom parameters to GameObjects and access them? 1 Answer
Instantiating thousands on start (loading custom levels) 3 Answers
Can't set the random position for an Instance. 1 Answer
Will Instantiating prefab with many components slow my game? 1 Answer