Problem with a Network script

I have made a script that does this:
Set other player’s layer to “OtherPlayer” and show them.
Set other player’s weapon layer to “OtherWeapon” and hide them.
Set own player’s layer to “OwnPlayer” and hide that layer.
Set own player’s weapon layer to “OwnWeapon” and show it.

so that each player has a weapon and arms models attached to a cameraHead GameObject and a soldier model in the player. I want to hide other’s weapon but show mine and hide my player’s weapon.

I set the Culling mask on my camera to hide “OtherWeapon” and “OwnPlayer”

and attached this script to my player prefab that get’s spawned.

using UnityEngine;
using System.Collections;

public class LayerChange : MonoBehaviour {
	
private Transform myTransform;
	
private Transform cameraHeadTransform;
	
private Transform soldierModelTransform;
	
//private Transform weaponTransform;


	
	void Start ()
	{
		myTransform = transform;
		
		cameraHeadTransform = myTransform.FindChild("CameraHead");
		
		soldierModelTransform = myTransform.FindChild("Soldier");
		
		//weaponTransform = cameraHeadTransform.FindChild("Weapons");
		
		if(networkView.isMine == true)
		{
			soldierModelTransform.gameObject.layer = LayerMask.NameToLayer("OwnPlayer");
			
			//weaponTransform.gameObject.layer = LayerMask.NameToLayer("OwnWeapon");
		}
		
		if(networkView.isMine == false)
		{
			Debug.Log("Run On Other Players");
			
			//soldierModelTransform.gameObject.layer = LayerMask.NameToLayer("OtherPlayer");
			
			//weaponTransform.gameObject.layer = LayerMask.NameToLayer("OtherWeapon");
		}
	}
}

This code only changes the gameobject’s layer and not it’s childrens layer. how would I change their layers as well?

BUMP

This kind of posts typically don’t get answered very much. By this kind I mean: “Here’s my code, here’s what it does, here’s what it should do, fix it!”
You need to be very specific about your problem so people can read it fast and suggest solution fast so you can try it. No one is going to download your code, test it, understand it, fix it and post it back.
You need to find what part of the code is causing the problem or working not as expected. You can use debugger or console logging to do this or you can keep stripping your script from specific parts and testing till you find the part that is not working as you expect it to.

You’re absolutely right, no one wants to do that and it’s stupid of me to make it sound like that’s what I want.

apparently the reason I couldn’t move was that I had removed a gameObject that was needed for everything else to work. fixed that and the layer get’s changed. but it only changes the layer of the gameObject and not the children’s layers…

I saw on another post a way to fix this but the code didn’t help me, I couldn’t figure out how it worked.

How do you change the layer of a gameobject and it’s children + their children and so on?

So you probably want to do something like:

void SetLayerForChildren(GameObject object)
{
   foreach (Transform child in object.transform)
   {
      child.gameObject.layer = ...
      //Also we should do this for children of children assuming that transform stores only first level children so:
      SetLayerForChildren(child.gameObject);
   }
}

SetLayerForChildren(this);

Thanks. With this and some editing i got it to work with my script. do you know how to call this function with a string so it sets the layer to that string?
This made some errors:

void SetLayerForChildren(GameObject _object, string layerName)
{
	foreach (Transform child in _object.transform)
	{
		child.gameObject.layer = LayerMask.NameToLayer(layerName);
		//Also we should do this for children of children assuming that transform stores only first level children so:
		SetLayerForChildren(child.gameObject);
	}
}

//And to call it:
SetLayerForChildren(soldierModelTransform.gameObject, "OwnPlayer");

Error:
Assets/Scripts/Client/LayerChange.cs(51,25): error CS1501: No overload for method SetLayerForChildren' takes 1’ arguments

I don’t understand why it acts like that. did I write something wrong in the line that calls the function?

What you posted seams fine. What the error is saying however is that in line 51 (or 25? not sure why there are two numbers there) you are calling SetLayerForChildren function and passing only one argument and there is no definition of SetLayerForChildren function that takes only one argument. Like you posted it takes two.
You have something like:

SetLayerForChildren(soldierModelTransform.gameObject);

in line 51 perhaps?

EDIT:
My mistake. In what you posted in line 7 you call:

SetLayerForChildren(child.gameObject);

You forgot to pass along the second argument, it should be:

SetLayerForChildren(child.gameObject, layerName);

Oh. wow. can’t believe I missed that one :confused: and I think what it means when it says (51, 21) is line 51 and text 21 or something, but it usually doesn’t work.

Thanks for the help Pirs01 :smile: