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 A-bee · Nov 17, 2013 at 12:41 AM · c#inheritancertsaccess

Problem with inheritance and access between scripts.

So I'm making an RTS game and I'm running into problems with inheritance. I'm going to strip to the basics to make the question more simple. So I have four scripts,

Building_Main.cs

BuildingA.cs which inherits from building_Main

Base_Unit.cs

Soldier.cs - which inherits from Base_unit. Building_Main has some methods that draw gui and builds the building queue which work perfectly.

So Building A's start code looks like

   public GameObject x;    
   void Start () {
       base.unit = x;
  }

so x is the unit building A will produce. x is a gameobject in my assets folder, with the soldier.cs script attached. I attached building_A to an actual gameobject in the world and dragged the prefab soldier from my asssets and put it in x. This is the Base_Unit script

     protected float health { get; set; }
     protected float speed { get; set; }
     protected int level { get; set; }
     internal float buildTime { get; set; }
   
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
     
     }
    

This is the Soldier.cs which inherits

  void Start () {
             base.health = 10;
             base.speed = 10;
             base.defense = 10;
             base.buildTime = 10;
         }

As you can see, soldier sets its building time to be 10.

The problem occurs in Building_main's addToQueue function whose code is below:

 public void addToQueue(GameObject bug)
     {
         buildQueue.Add(bug);
         Debug.Log("Adding to the queue, a build time of " + unit.GetComponent<Base_Unit>().buildTime); 
      
     }

Remember unit is what building_A sets to soldier in its start function. However buildtime comes to be 0 even thought it should be 10. Am I approaching this wrong? I have no idea how to proceed further. Please let me know if the question isn't clear enough.

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
Best Answer

Answer by salex100m · Nov 17, 2013 at 07:22 PM

I'm not 100% sure but i think you might not be using the solider as intended.

  base.unit = x;

That just creates a reference in your file pointing to another reference. But neither reference is actually pointing to an "actual" object. You might need to instantiate a solider, THEN set it to base.unit = x.

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 A-bee · Nov 18, 2013 at 06:14 PM 0
Share

Thanks that cleared it up.

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

18 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

A node in a childnode? 1 Answer

Disable Mac OSX 'Active Screen Corners' 2 Answers

Multiple Cars not working 1 Answer

Accessing methods from another script. C# 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