Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Overriding method calling itself instead of base method

Discussion in 'Scripting' started by WaitYes, Feb 5, 2018.

  1. WaitYes

    WaitYes

    Joined:
    Sep 5, 2016
    Posts:
    11
    I'm making a multiplayer spaceship game, like Artemis or Star Trek Bridge Crew, where different players control different parts of one ship. So I have it that when each player is created, the server then calls a function on the player called RpcRegisterShip, which takes the ship they are assigned to as a parameter. This ship is then used to get the ship's transform, the ship's rigidbody, or whatever else the player will need to interact with on the ship.

    It works just fine when I have each type of player, (pilot, gunner, engineer, etc.), have their own RpcRegisterShip function, but I'm trying to be a good programmer and implement inheritance. So I made a base class, "PlayerControls," and then I have "PilotControls," "GunnerControls," etc., inherit from that class.

    The base RpcRegisterShip gets some basic info, but each of the child classes override RpcRegisterShip so they can each get what special information they need. The overriding RpcRegisterShip still calls the base RpcRegisterShip though.

    What's weird is that the overriding RpcRegisterShip does not seem to be calling the base function. It might even be calling itself. What am I doing wrong?

    Base Class:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. abstract public class PlayerControls : NetworkBehaviour {
    7.  
    8.     protected GameObject ship;
    9.  
    10.     protected Transform shipTransform;
    11.  
    12.  
    13.     [ClientRpc]
    14.     public virtual void RpcRegisterShip (GameObject receivedShip) {
    15.  
    16.         Debug.Log("Register Ship Base");
    17.  
    18.         if(!isLocalPlayer) {
    19.             return;
    20.         }
    21.  
    22.         ship = receivedShip;
    23.  
    24.         shipTransform = ship.GetComponent<Transform>();
    25.  
    26.     }
    27.  
    28.     ...
    29.  
    30. }
    Pertinent part of the derived class:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class PilotControls : PlayerControls {
    7.  
    8. ...
    9.  
    10.     private Rigidbody shipRigidbody;
    11.  
    12.     [ClientRpc]
    13.     public override void RpcRegisterShip (GameObject receivedShip) {
    14.  
    15.         if(!isLocalPlayer) {
    16.             return;
    17.         }
    18.  
    19.         Debug.Log("Register Ship Derived");
    20.         base.RpcRegisterShip(receivedShip);
    21.      
    22.         shipRigidbody = receivedShip.GetComponent<Rigidbody>();
    23.     }
    24.  
    25. ...
    26.  
    27. }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It looks all right to me. Do you not get the base debug statement?
     
  3. WaitYes

    WaitYes

    Joined:
    Sep 5, 2016
    Posts:
    11
    I don't get the base debug statement. It's the weirdest thing.
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Try removing the if clause about local player.
     
  5. WaitYes

    WaitYes

    Joined:
    Sep 5, 2016
    Posts:
    11
    And that did. That seems strange to me. Why would removing the if clause about local player, when the script should be on the local player, solve the problem?

    Anyway! Thanks for the help!
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Glad it worked out.