Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zerikscythe · Jul 18, 2014 at 12:02 PM · c#instantiateloadingcomponents

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!?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges