• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Showing different objects OnMouseDown 1 Answer

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

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

Error building Player: CommandInvokationFailure: Failed to re-package resources. 0 Answers

can I Cutoff/Blackscreen part of the unity camera screen? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges