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 cowmilk9 · Dec 06, 2013 at 10:09 AM · runtimereferencelistsinheritance

Inheritance Confusion

I'm trying to inherit from an Enemy class to a Guard class but NOTHING IS INHERITING! How does this all work??? I've been at this for hours. I just keep coming up with UnsignedReferenceExceptions and things aren't working. Here's my code:

 // Enemy.cs
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Enemy : MonoBehaviour 
 {
     protected NavMeshAgent navMeshAgent = null;
     public List<GameObject> pathNodes = new List<GameObject>();
     protected int currentPathNodeIndex = 0;
     public float pathNodeDistanceThreshold = 1.0f;
     public GameObject player = null;
     public float aggroRadius = 5.0f;
     public int delayRange = 0;
     public float timer = 0.0f;
 
     // Use this for initialization
     void Start () 
     {
         delayRange = Random.Range(2, 10);
         navMeshAgent = gameObject.GetComponent<NavMeshAgent>();
         if (navMeshAgent == null)
         {
             navMeshAgent = gameObject.AddComponent<NavMeshAgent>();
         }
         
         if (pathNodes.Count <= 0)
         {
             Debug.LogError("This sytem requires that at least one path node blah blah blah....");
         }
         
         player = GameObject.FindWithTag("Player");
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (player == null)
         {
             return;
         }
 
         if (navMeshAgent == null)
         {
             return;
         }
         
         if (pathNodes.Count <= 0)
         {
             return;
         }
     }
 }

 // Guard.cs
 
 // Here, Guard isn't inheriting the Player so he can't find him
 //Why? Shouldn't the Enemy grab the object with the tag, "Player" and therein allowing Guard to use it?
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Guard : Enemy 
 {
     // Use this for initialization
     void Start () 
     {
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (Vector3.Distance(gameObject.transform.position, player.transform.position) <= aggroRadius)
         {
             navMeshAgent.destination = player.transform.position;
         }
         
         else
         {
             navMeshAgent.destination = pathNodes[currentPathNodeIndex].transform.position;
             if (Vector3.Distance(gameObject.transform.position, navMeshAgent.destination) <= pathNodeDistanceThreshold)
             {
                 
                 timer = timer + Time.deltaTime;
                 if (timer >= delayRange)
                 {
                     timer = 0.0f;
                     currentPathNodeIndex++;
                     delayRange = Random.Range(2, 10);
                 }
                 
                 if (currentPathNodeIndex >= pathNodes.Count)
                 {
                     currentPathNodeIndex = 0;
                 }
             }
         }
     }
 }

Why isn't this working? Also, how do I put prefabs into lists at runtime? I need to do that so I can create new PathNodes and Enemy is able to grab those nodes and put them in the list. Any help would be appreciated. Inheritance just confuses me greatly......

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
1

Answer by GameVortex · Dec 06, 2013 at 10:15 AM

Unity will only call the first Start() and Update() functions it find starting from the Child. So in your case the Start() and Update() in Guard will be used but not the ones in Enemy. Your player object never gets a value because the value is set in the Start() function in the Enemy which does not get called. A solution would be to add base.Start() and base.Update() in the Guard functions:

 void Start () 
 {
     base.Start();
 }
 
 void Update()
 {
     base.Update();
 }

Remember to change the Start() and Update() functions in Enemy to be either public or protected so that Guard has access to them.

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 cowmilk9 · Dec 06, 2013 at 10:38 AM 0
Share

Great. That did it. But I still get some warnings that say something about Guard.Start() hiding Enemy.Start() or something. It doesn't affect the program, but I don't know if that will come to bite me in the future. But thank you very much for the help. $$anonymous$$akes more sense now. Now, do you know of any way to make it so PathNodes that are created at runtime are programatically referenced in the pathNodes list? It doesn't let me use FindWithTag() with Lists, so I don't know what to do. Thanks again!

avatar image robhuhn · Dec 06, 2013 at 10:53 AM 1
Share

You're not overriding Start and Update properly. If you want to declare methods overridable you should add virtual to that method and change the modifier to protected to make it visible for derived classes. Declare the overriding method with an override

In Enemy:

 virtual protected void Start () ...

In Guard:

 override protected void Start () 
 {
     base.Start();
 }
avatar image cowmilk9 · Dec 06, 2013 at 11:09 AM 0
Share

Oh I see. Thank you. It's still a bit confusing, but I'll figure it all out later. Thanks again.

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

Restore list elements at termination 0 Answers

Best practice for assigning class reference at runtime? 0 Answers

Instantiating scripts with references to prefabs loses the reference. 1 Answer

Attaching player control script at runtime 0 Answers

ArgumentException: GetComponent requires that the requested component 'List`1' derives from MonoBehaviour or Component or is an interface. 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