Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by ProNoob2 · Jun 20, 2019 at 12:54 PM · unityeditorgameobjectsarray of gameobjectsgetderived-class

Get gameObjects from a protected GameObject array of base class in derived class

I have a StageScript in which I am filling an array in the inspector named UnorthodoxSeats marking it as [SerializeField] protected GameObject[] UnorthodoxSeats in the script. When the script runs, after some time a Character is instantiated to move towards the seat. The Character has also a script named CharacterScript which is the derived class of StageScript. Now, In the CharacterScript whenever I get UnorthodoxSeats.Length it shows 0. I am confused why?

Any help would be awesome.

Comment
Add comment · Show 5
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
avatar image Hellium · Jun 20, 2019 at 01:07 PM 0
Share

Can you edit your question and provide more code (class A and B), a screenshot of the inspector with your gameObject holding the component B and the full message in the console?

avatar image Bunny83 · Jun 20, 2019 at 01:08 PM 0
Share

Sorry but the way you describe your class definitions and the declaration of your array is not enough to figure out what you might have done wrong. Why don't you post the relevant code? Are you sure that you do not overwrite the array somewhere in Start or Awake?

avatar image ProNoob2 Bunny83 · Jun 20, 2019 at 01:38 PM 0
Share
 `public class StageScript
 {
  [SerializeField] protected GameObject[] UnorthodoxSeats;
 
 // not using awake
 // start only have invokeRepeating method but that method is not related to UnorthodoxSeats
 }
 
 public class CharacterScript :  StageScript
 {
         private Start()
         {
                Debug.Log(UnorthodoxSeats.Length);
         }
 }`



avatar image Glurth ProNoob2 · Jun 20, 2019 at 07:50 PM 0
Share

If the base class has a Start method also (even one that doesn't touch the array), you will need to specify the override keyword for the Start method in the derived class, AND invoke the base class's version manually e.g. base.Start();, from inside it.

As a sanity check- I suggest you display the array length in both base and derived class versions of Start: I expect they will display the same results; if so, this a clue that one of your assumptions is incorrect.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Kennai · Jun 21, 2019 at 06:57 AM

Hi, @OsamaSany! I suppose that you work with two different instances.
One instance is type of StageScript, where you filled field.
And another instance is type of CharacterScript, which has empty field, because it is other instance.
If you need to share field between classes - use static field!

Comment
Add comment · Show 1 · Share
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
avatar image ProNoob2 · Jun 21, 2019 at 09:08 AM 0
Share

Yes I think it was that. I made the array public and created a place holder for Stage in the character script and accessed it as the characters great grand parent and accessed the stage script and array from that place holder and it is working fine now.

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

109 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

trying to get gameobjects assigned to Array[x,y] 1 Answer

Showing different objects OnMouseDown 1 Answer

How to deal with 10,000s immobile gameobjects at once? 0 Answers

Starting new 3D project in unity 0 Answers

How to remove grayscreen? 0 Answers


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