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 MattSawrey · Jan 10, 2015 at 02:01 PM · getcomponentinheritanceaccessing

Inheriting variables from common parent class on two different gameobjects

Here's the structure of my problem.

In my scene I have two snake gameobjects, each controlled by a player using a turn-based system. Each of these snake gameobjects contains a c# script with a class called "Snake_BodyCreator", which creates the snake's body. This class inherits from another class in another script, called "Snake", which contains all of the properties common to a snake. The "Snake" class is not attached to any gameobject in the scene.

I then have a third class called "Snake_Movement". This class is also attached to each of the gameobjects, and is enabled through my "TurnController" script, when it is a particular player's turn.

Here's how it looks in the inspector:

Snake1(Gameobject)

  • Snake_BodyCreator(Inherits from "Snake" class)

  • Snake_Movement(I want this to access the Snake_BodyCreator class' properties

Snake2(Gameobject)

  • Snake_BodyCreator(Inherits from "Snake" class)

  • Snake_Movement(I want this to access the Snake_BodyCreator class' properties

My problem is trying to access the "Snake_BodyCreator" script from the "Snake_Movement" script. Here's the relevant code from the "Snake_Movement" script that accesses the "Snake_Body" script:

   public class Snake_Movement : MonoBehaviour {
     
         Snake_BodyCreator  snakeBody;
     
         void OnEnable () 
         {
             snakeBody = gameObject.GetComponent<Snake_BodyCreator>();
             if(snakeBody == null)
             {
                 Debug.Log("Can't find Snake_BodyCreator script");
             }
         }
     
         void MoveUp()
         {
             //If at the top of the grid
             if(snakeBody.SnakeGridCoOrdXY[1] == gridPos.GetLength(1)-1)
             {//Do something}
         }

As you can see, I have a Debug.log check to make sure that "snakeBody" is actually accessing the "Snake_BodyCreator" script, and it is. But when I use it to try and access any public property from the "Snake_BodyCreator" script in the "MoveUp()" function (in this example, "SnakeGridCoOrdXY[1]"), I get a NullReferenceException error, stating that this reference is not set to an instance of an object. I don't understand how it isn't. I wondered if it might have something to do with the fact that I have two "Snake_BodyCreator" instances in the scene that inherit from the common "Snake" class.

Any answers would be much appreciated.

Matt

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hypnoticmeteor · Jan 10, 2015 at 02:20 PM

I am not going to write the entire code. You need add all the programs that you use. So snake must be in the scene. Add an empty gameobject and assign Snake.cs to it. Make sure all the variables that you want to use in another script are public. The second script should now either find or reference this gameobject which has "Snake.cs" attached to it. This is done by

 GameObject SnakeGO;//assign in editor
 Snake snakePointer;//instance of the script
 
 void Start()
 {
 //if assigned in editor use this
 snakePointer = SnakeGO.gameObject.GetComponent<Snake>();
 //if not assigned but is available in the editor
 snakePointer = GameObject.Find("Snake").GetComponent<Snake>();
 //if not in editor
 //the error of null pops up
 }
 
 snakePointer.publicVariable;

Follow the same procedure for the others.

Comment
Add comment · Show 3 · 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 MattSawrey · Jan 10, 2015 at 03:36 PM 0
Share

I thought that you could inherit from a class without having it present in the scene. $$anonymous$$y Snake_BodyCreator script inherits from it as below.

 public class Snake_BodyCreator : Snake { //rest of the script }

Also, the "Snake_$$anonymous$$ovement" class isn't trying to inherit properties from the "Snake" class. It's trying to access things from the "Snake_BodyCreator" class.

avatar image hypnoticmeteor · Jan 10, 2015 at 05:27 PM 0
Share

You need to have it in the scene.

avatar image MattSawrey · Jan 10, 2015 at 06:10 PM 0
Share

Thanks, but you can. I solved the issue. Turns out that I hadn't declared my properties correctly in the "Snake_BodyCreator". It's perfectly possible to inherit from a class written in a script that isn't in the scene.

Thanks anyhow.

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

26 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

Related Questions

C# accessing an array from a script in a different gameobject outside of the awake function 0 Answers

Using GetComponent with a subclass 3 Answers

Difficulties accessing other components 4 Answers

Help with accessing scoring system variable C# plz. 3 Answers

NullReferenceException. GetComponent dose not work properly 2 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