- Home /
Highlight other players - Networking
So my networking is going well so far except for one thing. I have two scripts that worked on other objects when the game wasn't built for networking, but they no longer function properly and behave oddly.
The effect of the first script is to highlight objects blue when your cursor goes over them.
The effect of the second script is that if you left mouse click on the target, it stays highlighted blue (except if it's yourself).
When I build and play a network game, they function fine for highlighting my own player, however, when I put my mouse over another player, it turns them black and they just stay that way even if the cursor goes off of them.
I can still click on the enemies and it targets them and I can shoot at them, the odd thing is that they turn black when the script should be highlighting their original color to a blue. Both scripts are attached to the player object.
I want the other players to be highlighted blue for just the player doing the on mouse over and the targeting.
HighlightScript (first script)
private float blueMultiply = 3.50f;
private float redGreenMultiply = 0.50f;
private Color originalColor;
private void Start()
{
if(networkView.isMine == true )
{
originalColor = gameObject.renderer.material.color;
}
else
{
enabled = false;
}
}
void OnMouseEnter()
{
AddHighlight();
}
void OnMouseExit()
{
RemoveHighlight();
}
private void AddHighlight()
{
float red = originalColor.r * redGreenMultiply;
float blue = originalColor.b * blueMultiply;
float green = originalColor.g * redGreenMultiply;
gameObject.renderer.material.color = new Color(red, green, blue);
}
private void RemoveHighlight()
{
gameObject.renderer.material.color = originalColor;
}
Targeting Script:
//targeting
if (Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit, Mathf.Infinity)){
if (hit.transform.tag == "Player"){
if (hasTarget)
{
RemoveHighlightOnTarget();
}
if (hit.transform.gameObject != gameObject)
{
go = hit.transform.gameObject;
target = go.transform;
hasTarget = true;
originalColor = go.renderer.material.color;
AddHighlightOnTarget();
}
//Debug.Log (go.renderer.material.color);
}
}
}
if (go == null)
{
hasTarget = false;
}
Any insight on this would be greatly appreciated! Thanks
Also part of the targeting script:
//These Variables
private float blue$$anonymous$$ultiply = 3.50f;
private float redGreen$$anonymous$$ultiply = 0.50f;
private Color originalColor;
//Highlight Function on target
private void AddHighlightOnTarget()
{
float red = originalColor.r * redGreen$$anonymous$$ultiply;
float blue = originalColor.b * blue$$anonymous$$ultiply;
float green = originalColor.g * redGreen$$anonymous$$ultiply;
go.renderer.material.color = new Color(red, green, blue);
}
//Remove highlight from target
private void RemoveHighlightOnTarget()
{
go.renderer.material.color = originalColor;
}
What happens if you remove the else branch in the start method of the first script?
I'll give it a shot! I also have a similar else branch in the second script so I'll try removing that one as well, or combining that part of the script.
Nothing, other players still turn black. The owned player highlights properly still.
I combined the scripts into one, here's the updated one with the else taken out:
using UnityEngine;
using System.Collections;
public class HighlightScript : $$anonymous$$onoBehaviour {
private float blue$$anonymous$$ultiply = 3.50f;
private float redGreen$$anonymous$$ultiply = 0.50f;
private Color originalColor;
private Color originalColor2;
public bool hasTarget = false;
public Transform target;
public GameObject go;
//Raycast targetting
public RaycastHit hit;
public Ray ray;
private void Start()
{
if(networkView.is$$anonymous$$ine == true )
{
originalColor = gameObject.renderer.material.color;
}
/*else
{
enabled = false;
}*/
}
void Update()
{
//targeting
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit, $$anonymous$$athf.Infinity)){
if (hit.transform.tag == "Player"){
if (hasTarget)
{
RemoveHighlightOnTarget();
}
if (hit.transform.gameObject != gameObject)
{
go = hit.transform.gameObject;
target = go.transform;
hasTarget = true;
originalColor2 = go.renderer.material.color;
AddHighlightOnTarget();
}
}
}
//Debug.Log (go.renderer.material.color);
}
if (go == null)
{
hasTarget = false;
}
}
void On$$anonymous$$ouseEnter()
{
AddHighlight();
}
void On$$anonymous$$ouseExit()
{
RemoveHighlight();
}
private void AddHighlight()
{
float red = originalColor.r * redGreen$$anonymous$$ultiply;
float blue = originalColor.b * blue$$anonymous$$ultiply;
float green = originalColor.g * redGreen$$anonymous$$ultiply;
gameObject.renderer.material.color = new Color(red, green, blue);
}
private void RemoveHighlight()
{
gameObject.renderer.material.color = originalColor;
}
//Highlight Function on target
private void AddHighlightOnTarget()
{
float red = originalColor2.r * redGreen$$anonymous$$ultiply;
float blue = originalColor2.b * blue$$anonymous$$ultiply;
float green = originalColor2.g * redGreen$$anonymous$$ultiply;
go.renderer.material.color = new Color(red, green, blue);
}
//Remove highlight from target
private void RemoveHighlightOnTarget()
{
go.renderer.material.color = originalColor2;
}
}
Answer by Jamora · Jun 04, 2013 at 09:03 PM
If the remote computer network instantiates their player, then the if
on line 27 is never entered on your computer, and a possibly null Color is used.
Interesting. If I do that, then the colors work. But now there is some weird behavior going on as for click them for targeting and being able to click yourself.
Now I just need to figure out why clicking myself highlights the other player and how to fix that and so forth...
The next thing I would try is to use this Start method private void Start() { if(networkView.is$$anonymous$$ine == false ) { originalColor = gameObject.renderer.material.color; } else { enabled = false; } }
Your answer
Follow this Question
Related Questions
Deleting a player over the network 1 Answer
Assigning Player Numbers 0 Answers
Unity networking tutorial? 6 Answers
Changing two different objects renderer colour 1 Answer
How to check when a new client/local player is loaded? 1 Answer