Hey guys having troubles enabling a script component of a child of a child
Trying to enable PlayerShooting in GunBarrelEnd
CurrentPlayer PlayerWrapper GunBarrelEnd
I tried doing something like this below but I get hit with error foreach statement cannot operate on variables of type PlayerMovement' because it does not contain a definition for GetEnumerator’ or is not accessible
I don’t understand what goes in the ********
The example online I was looking at shows Camera but I’m not looking for a camera im looking for a script
foreach(******* ps in GetComponentInChildren<PlayerShooting>())
ps.enabled = true;
shouldn’t I beable to some how get PlayerWrapper
and then use that to get GetComponentInChildren of PlayerWrapper?
so I don’t have to do loop through.
Maybe someone can help set me straight on my logic here.
GetComponentInChildren uses depth first, from CurrentPlayer you won’t find the PlayerShooting on GunBarrelEnd that way. GetComponentsInChildren however searches for all of them - use that one instead.
For the ******* just use “var” or “PlayerShooting”. “var” will automatically cast the correct type while you can also directly define it using “PlayerShooting”.
As mentioned by Glocken, you need to use GetComponentsInChildren instead of GetComponentInChildren. The first returns the array containing all components of type T, and the later returns T.
Edit: Reread your second post. You are using GetComponentsInChildren now. Are you sure you are not deactivating them somewhere else?
Perhaps add a Debug.Log statement to PlayerShooting’s OnDisable find out if something else in your project is disabling it.
Your code looks fine. It should work. The only thing that can interfere is that the gameobject to which your script is attached is disabled. If the gameobject itself is disabled, then the script attached to it will not be activated even if you set enabled to true because GetComponentsInChildren ignores inactive gameobjects unless you tell it to look for them.
So change your code to this:
foreach(PlayerShooting ps in GetComponentsInChildren<PlayerShooting>(true)){
//First activate the gameObject. Otherwise your script is as good as dead.
ps.gameObject.SetActive(true);
//Enable the script.
ps.enabled = true;
}
I debugged to make sure the if(photonView.isMine){ was coming up true on all 3 scripts and it is.
ok so I have code that I’m trying to enable the PlayerShooting component on 3 separate scripts each on their own game object. (3 scripts cause I’m trying to get one to work lol)
Also to note. All game objects are enabled. Once I start the game if I go to the clone CurrentPlayer and enable PlayerShooting it works.
CurrentPlayer PlayerCamera PlayerWrapper Gun GunBarrelEnd PlayerShooting // this is a component not a game object Player CurrentPlayer Component This is attached to game object CurrentPlayer
```
**using UnityEngine;
using System.Collections;
public class CurrentPlayer : Photon.MonoBehaviour {
void Start () {
if (photonView.isMine) {
GetComponentInChildren<Camera> ().enabled = true; // this works
GetComponentInChildren<CameraFollow> ().enabled = true; // this works
GetComponentInChildren<PlayerMovement> ().enabled = true; // this works
foreach(PlayerShooting ps in GetComponentsInChildren<PlayerShooting>())
ps.enabled = true; // this does not works
}
}
}** ```
I just now notice I get a error when trying to enable it on this script below Object reference not set to an instance of an object
on line “GetComponentInChildren ().enabled = true;”
PlayerNetworkMover Component This is attached to game object PlayerWrapper
using UnityEngine;
using System.Collections;
public class PlayerNetworkMover : Photon.MonoBehaviour {
void Start(){
if (photonView.isMine) {
GetComponentInChildren<PlayerShooting> ().enabled = true;
}else{
StartCoroutine ("UpdateData");
}
}
}
PlayerShooting Component This is attached to game object PlayerShooting
using UnityEngine;
using System.Collections;
public class PlayerShooting : Photon.MonoBehaviour
{
void Start(){
if (photonView.isMine) {
GetComponent<PlayerShooting> ().enabled = true;
}
}
}
Well, you really don’t need the last script. You are basically telling the script to enable itself but Start won’t be called on it because its disabled.
Ok so this is what Im assuming whats going on now then.
The first script isn’t giving an error since its looping through looking for it and find no matches.
The Third script didn’t give an error cause it wasn’t being ran lol
The 2nd script is giving an error
so this is the reason why its not working then. It can’t find it for some reason Object reference not set to an instance of an object
First off I created a new script called PlayerWrapper which is component of PlayerWrapper
I got rid of Photon.MonoBehaviour and of course the if statement around it.
This still fails to enable it
using UnityEngine;
using System.Collections;
public class PlayerWrapper : MonoBehaviour {
void Start(){
//if (photonView.isMine) {
GetComponentInChildren<PlayerShooting> ().enabled = true;
//}
}
}
Is it possible that its trying to enable the PlayerShooting component before the CurrentPlayer gets Instantiate?
so that’s why its not part of any object because CurrentPlayer isn’t in the scene yet?
The second script is giving an error because GetComponentInChildren is returning null, i.e. it can’t find the component. The problem is the same as the first script.
You are ticking all the right boxes here. I would love to help you but its difficult to pinpoint without looking at the project. Can you create an empty project with just the troublesome components, and share it on GDrive etc.?
Naah. You are calling them in Start. So object initialization shouldn’t be a problem. Awake comes before start and Awake is called after all objects are initialized. So, trying to reference them in start shouldn’t be a problem
Ya I’ll do that man. really appreciate the help. This is driving me insane lol.
I’ll create a project with only the scenes no other assets but scripts and anything those need. See if it still is doing it then.
ya thanks a lot pmaloo
I was following a tutorial and I dragged another file from another location into the PlayerShooting component not knowing the effect it would make on the namespace.