Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by THESUBMATRIX · Apr 17, 2017 at 03:14 PM · c#functionlayers

Function will only set layer in some cases, Why is this?

I created a Global Functions script for functions I will need across multiple scripts and to be used in many instances (In this case, I created a function to recursively rename a component and all of its child components to a layer) however, I cannot figure out where I am going wrong with this. I have attached the script that creates the function here:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
     
     public class GlobalFunctions : MonoBehaviour {
         public static void SetLayerRecursively(GameObject obj, int newLayer)
         {
             //Debug.Log("Setting Layer");
             obj.layer = newLayer;
             foreach (Transform child in obj.transform)
             {
                 SetLayerRecursively(child.gameObject, newLayer);
             }
     
         }
     }
 

This is the function that I am having problems with. I call this function in 2 locations at the current moment. The first call I make, the function works as intended

 using UnityEngine;
 using UnityEngine.Networking;
 
 public class PlayerSetup : NetworkBehaviour
 {
 
     [SerializeField]
     Behaviour[] componentsToDisable;
 
     [SerializeField]
     string RemoteLayerName = "RemotePlayer";
 
     [SerializeField]
     string DontDrawLayerName = "DontDraw";
 
     [SerializeField]
     GameObject PlayerGraphics;
 
     [SerializeField]
     GameObject PlayerUIPrefab;
     private GameObject PlayerUIInstance;
 
     Camera sceneCamera;
 
     void Start()
     {
         if (!isLocalPlayer)
         {
             DisableComponents();
             AssignRemoteLayer();
 
         }
         else
         {
             sceneCamera = Camera.main;
             if (sceneCamera != null)
             {
                 sceneCamera.gameObject.SetActive(false);
             }
 
             GlobalFunctions.SetLayerRecursively(PlayerGraphics, LayerMask.NameToLayer(DontDrawLayerName));
             PlayerUIInstance = Instantiate(PlayerUIPrefab);
             PlayerUIInstance.name = PlayerUIPrefab.name;
 
         }
         GetComponent<PlayerManager>().Setup();
     }
     public override void OnStartClient()
     {
         base.OnStartClient();
 
         string _NetID = GetComponent<NetworkIdentity>().netId.ToString();
         PlayerManager _player = GetComponent<PlayerManager>();
 
         GameManager.RegisterPlayer(_NetID, _player);
     }
 
     void AssignRemoteLayer()
     {
         gameObject.layer = LayerMask.NameToLayer(RemoteLayerName);
     } 
     void DisableComponents()
     {
         for (int i = 0; i < componentsToDisable.Length; i++)
         {
             componentsToDisable[i].enabled = false;
         }
     }
     void OnDisable()
     {
         Destroy(PlayerUIInstance);
         if (sceneCamera != null)
         {
             sceneCamera.gameObject.SetActive(true);
         }
         GameManager.UnRegisterPlayer(transform.name);
     }
 
 }

However, when I call this function a second time, the layer does not get set.

 using UnityEngine.Networking;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerShoot : NetworkBehaviour {
 
     private const string PLAYER_TAG = "Player";
 
     [SerializeField]
     private PlayerWeapon weapon;
 
     [SerializeField]
     GameObject WeaponGFX;
 
     [SerializeField]
     private string weaponLayerName = "Weapons";
 
     [SerializeField]
     private Camera playerCam;
 
     [SerializeField]
     private LayerMask mask;
 
     void start()
     {
         if (playerCam == null)
         {
             Debug.LogError("PlayerShoot: No Camera Reference");
             enabled = false;
         }
         GlobalFunctions.SetLayerRecursively(WeaponGFX, LayerMask.NameToLayer(weaponLayerName));
         
     }
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             Shoot();
         }
     }
     [Client]
     void Shoot()
     {
         RaycastHit _hit;
         if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out _hit, weapon.range, mask))
         {
             if(_hit.collider.tag == PLAYER_TAG)
             {
                 CmdPlayerShot(_hit.collider.name, weapon.damage);
             }
         }
     }
 
     [Command]
     void CmdPlayerShot (string _playerID, int _damage)
     {
         Debug.Log(_playerID + " has been shot.");
 
         PlayerManager _player = GameManager.GetPlayer(_playerID);
         _player.RpcTakeDamage(_damage);
     }
 
 }
 

I am not sure exactly why this happens. Please take a look, I will upload any other assets you need to see if you ask. Thank you for your help!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hexagonius · Apr 17, 2017 at 03:59 PM

Because it's Start, not start ;)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

337 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a credits button? 1 Answer

My function just doesn't do anything when I put it in onclick is there a problem with my script? 1 Answer

How do I convert this into C# and where should this be added? 1 Answer

Turn on and off a Cubes(GameObject) Is Trigger. 0 Answers

Problem with decrement 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges