#NullReferenceException: Object reference not set to an instance of an object with prefab

2 messages · Page 1 of 1 (latest)

old hare
#

Relevant Code:

TestGun.cs

using UnityEngine;
using FishNet;
using FishNet.Connection;
using FishNet.Managing.Scened;
using FishNet.Object;
public class TestGun : Gun
{

    public GameObject projectilePrefab; // Assign this in the Unity Editor

    public override void InitializeSpec()
    {
        this.subtype = GameConstants.GunType.Pistol;
        this.rarity = GameConstants.Rarity.Uncommon;
        this.specName = "Test Gun";
        this.baseDamage = 10;
        this.attackSpeed = 20;
        this.modelPath = "Weapons/Guns/SMG/SMG";
        this.armorPen = 0.5f;

        // Assets/Resources/Prefabs/TestProjectilePrefab.prefab
        projectilePrefab = Resources.Load(TestProjectile.prefab_path) as GameObject;
    }



    public override void PerformMainFunction()
    {
        ServerPerformMainFunction(); // <----- line 29, errors here
    }


    [ServerRpc]
    public void ServerPerformMainFunction()
    {
        RpcPerformMainFunction();
    }

    [ObserversRpc]
    public void RpcPerformMainFunction()
    {
        GameObject projectileObject = Instantiate(projectilePrefab, transform.position, transform.rotation);
        TestProjectile projectile = projectileObject.GetComponent<TestProjectile>();
        projectile.Initialize(
            position: transform.position,
            rotation: transform.rotation,
            damage: this.baseDamage,
            speed: 10,
            procChance: 10,
            lifetime: 3,
            parentEntity: this.parentEntity,
            parentSpec: this
        );
    }
}
#

PlayerInputHandler.cs

using UnityEngine;
using FishNet.Object;

public class PlayerInputHandler : NetworkBehaviour
{
    private Player player; // Reference to the Player component
    private UIManager uiManager; // Reference to UIManager
    private DroppedItem currentDroppedItem; // Currently detected dropped item

    // KeyCodes for activating specs
    private KeyCode primarySpecKey;

    public override void OnStartClient()
    {
        base.OnStartClient();
        if (IsOwner)
        {

        }
        else
        {
            GetComponent<PlayerInputHandler>().enabled = false;
        }
    }
    void Start()
    {
        player = GetComponent<Player>();
        if (player == null)
        {
            Debug.LogError("PlayerInputHandler: No Player component found on the GameObject.");
        }
        // Initialize key bindings from GameConstants
        primarySpecKey = (KeyCode)GameConstants.SpecActivationKeys.Primary;
    }

    void Update()
    {
        if (!IsOwner) return; // Only handle input for the local player

        CheckForDroppedItem();

        // Check for spec activation using the assigned keys
        if (Input.GetKeyDown(primarySpecKey) && player.specs.Count > 0)
        {
            ActivateSpec(0); // line 55, errors here <----
        }

    }



    void ActivateSpec(int index)
    {
        if (player.specs != null && index < player.specs.Count)
        {
            SpecBase spec = player.specs[index];
            spec.PerformMainFunction(); // line 99 <- errors here
        }
        else
        {
            Debug.LogError("Spec index out of range or specs list is null: " + index);
        }
    }

}