- Home /
How can I make code work on a disabled object
I am working on a multiplayer FPS. In the FPS perspective you see your gun and your hands. Other players will see a soldier holding a gun. I want to add a mouse look to the spine so when you look up or down the soldier will also look up and down. I cant seem to do this if I disable the spines parent. There is a player handler running the show, it turns on or off gameobjects and scripts if the networkView is yours or not, these include the FPS view and the soldier view, any help would be appreciated, I could turn each of the soldiers objects off but that would be not productive.
InvokeRepeating() does not get disabled when a game object is disabled. I don't understand your full setup, but you might be able to execute you mouse look code in an InvokeRpeating().
Heres the code to the PlayerHandler (JS) (tpsView is what other people see you as)
#pragma strict
var fpsView : GameObject;
var tpsView : GameObject;
function Start () {
Screen.lockCursor = true;
Screen.showCursor = false;
if(networkView.is$$anonymous$$ine == true){
fpsView.active = true;
tpsView.active = false;
transform.GetComponent($$anonymous$$ouseLook).enabled = true;
transform.GetComponent(FPS$$anonymous$$ovement).enabled = true;
transform.GetComponent(DisconnectButton).enabled = true;
}else{
fpsView.active = false;
tpsView.active = true;
transform.GetComponent($$anonymous$$ouseLook).enabled = false;
transform.GetComponent(FPS$$anonymous$$ovement).enabled = false;
transform.GetComponent(DisconnectButton).enabled = false;
}
}
function Update () {
if(networkView.is$$anonymous$$ine == true){
fpsView.active = true;
tpsView.active = false;
transform.GetComponent($$anonymous$$ouseLook).enabled = true;
transform.GetComponent(FPS$$anonymous$$ovement).enabled = true;
transform.GetComponent(DisconnectButton).enabled = true;
}else{
fpsView.active = false;
tpsView.active = true;
transform.GetComponent($$anonymous$$ouseLook).enabled = false;
transform.GetComponent(FPS$$anonymous$$ovement).enabled = false;
transform.GetComponent(DisconnectButton).enabled = false;
}
}
And I am using the $$anonymous$$ouseLook.cs that came with unity
I found that using Invoke will still run the invoked method once it's time to, even if the object (whose script component the invoked method is on) has since been disabled sometime after the Invoke was called.
Answer by venusboysatish · Oct 07, 2013 at 07:51 AM
You can not make a script run on a disabled object. instead of disabling the object, disable the components that you don't want to act upon. for example, if you don't want it to be displayed in the game, disable its Renderer component, if you don't want it to collide, disable its collider, and so forth. hope this helps.
But I want other people to see you and have the bullets damage you as well.