script calls on other monsters/players hp/etc

I’m new to networking and im just trying to save a few headaches by asking for some direction. So, what im asking is, i have a script set up to damage a object (monster) in game and other players can damage it till it reaches 0 and it dies but set it up on multiple monsters and they all get damaged.

I sorta of realize why its doing this but don’t know how to fix it off the top of my head. Need to set it up so only the script on the monster that’s getting dmged is getting called.

Skip to latest post

How you define the variable for the monster health?
Sound like you put it as a global variable.

Ah I have the variable set up as a static var. I think thats why its affecting all other monster health. Let me see if I can switch these around.

For me, if I don’t have to define the value in the editor, I will just make the variable private.

But maybe different monster have different health level, you might just make it public for that particular game object.

Edit8: Can’t get the swordReady variable to fire off or work. Has to do with the way ive set up the referencing.

Script Enemy_Attacked Code:

    function Start () {
     enemyLogicInstance = GetComponent(EnemyLogic);
     MeleeInstance = GetComponent(Melee);
}

function Update () {

}

	function OnTriggerEnter (col : Collider)
	
	{
		if(Melee.swordReady == true)
		{
		if(col.tag == "Weapon")
		{
		networkView.RPC("RemoveHealth", RPCMode.All);
		Melee.swordReady = false;
		}
	}
}



	@RPC
	function RemoveHealth(){
	enemyLogicInstance.Enemy_Health -= 25;
	print(enemyLogicInstance.Enemy_Health);
	}
Script Melee Code:

    public var Enemy_Attacked_Instance;
     
    
    function Start () {
     Enemy_Attacked_Instance = Enemy_Attacked;
     var swordReady : boolean = true;
    }
     
    function Update ()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            swordReady = true;
            networkView.RPC("SyncAttackAnimation", RPCMode.All);
            StartCoroutine("DisableSwordReady");
        }
    }
    
    function DisableSwordReady()
    {
    
    	yield WaitForSeconds(1);
    	swordReady = false;
}    	
	@RPC
	function SyncAttackAnimation(){
		    animation.Play("Attack");
            animation["Attack"].wrapMode = WrapMode.Once;
            }

(EDIT: Im going to download some fps examples off the asset store and see if it points out what im doing wrong)

I really hate to bump this thread but i’ve been working on this for 7 hours now and I still can’t get this script fully working :sad:. I’m having serious problems setting up the swordReady variable to work properly. If i take it out, everything works but its needed so I don’t dmg things from just bumping into them with my sword.

I’ve tried 3 different ways of scripting the above it either doesn’t fire or I get compile errors before or after launch.

What happened if you comment out the Melee.swordReady = false; in enemy_attacked script?

Commented that line out. It doesn’t affect anything and doesn’t do anything still so I assume that the variable just isnt really doing anything. There must be some syntax i’m not familiar that I need to be using to set this up. Makes sense since i’ve swapped the code around between those 2 scripts so that the swordready var originated on the melee script and when I did that it was throwing null object reference errors.

Thx for the attention though Wiguan =)

This is the original way I tried doing it that actually gives the errors. It’s probably a step closer to achieving the end product since this one always has 1 error in it so I know i’m missing something.

Erorr:NullReferenceException: Object reference not set to an instance of an object
Meleeinstance is null. I think the problem is is that i’m trying to call this variable thats on the player and not on the monster. Thats why enemylogic instance works because its on the enemy to.

Enemy Attacked.js On enemy

var enemyLogicInstance;
public var MeleeInstance;


function Start () {
enemyLogicInstance = GetComponent(EnemyLogic);
Meleeinstance = GetComponent(Melee);
}




function Update () {

}

	function OnTriggerEnter (col : Collider)
	
	{
		if(MeleeInstance.swordReady == true)
		{
		if(col.tag == "Weapon")
		{

		networkView.RPC("RemoveHealth", RPCMode.All);
		MeleeInstance.swordReady = false;
		}
	}
}


	@RPC
	function RemoveHealth(){
	enemyLogicInstance.Enemy_Health -= 25;
	print(enemyLogicInstance.Enemy_Health);
	}

Melee.js On player

     public var Enemy_Attacked_Instance;
     var swordReady : boolean = false;
    
    function Start () {
     Enemy_Attacked_Instance = Enemy_Attacked;

    }
     
    function Update ()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            swordReady = true;
            networkView.RPC("SyncAttackAnimation", RPCMode.All);
            StartCoroutine("DisableSwordReady");
        }
    }
    
    function DisableSwordReady()
    {
    
    	yield WaitForSeconds(1);
    	swordReady = false;
}    	
	@RPC
	function SyncAttackAnimation(){
		    animation.Play("Attack");
            animation["Attack"].wrapMode = WrapMode.Once;
            }

I figured out what the problem is about now but not how to completely fix it. I’ll probably remake the thread and post the specifics in hopes of getting it completely solved since i’ve been at this for about 15 hours today (took a 2 hour break at the 10 hour mark :.)

(actually i may have a fix. i’ll apply it in the morning and see what happens. Think I just need to do abit of switching around)