- Home /
Question by
Reece2012FPS · Sep 26, 2012 at 01:40 PM ·
c#gameobjectactivatedeactivate
Activating/Deactivating GameObject Problem :(
Im trying to diables my sniper when aiming down the sights but when i release the left mouse button the sniper does not re-activate.
Please Help. C# Code...
using UnityEngine;
public class vp_FPSSniper : MonoBehaviour
{
public GameObject Sniper = null;
void Start()
{
Update ();
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
Sniper.SetActiveRecursively(false);
} else {
Sniper.SetActiveRecursively(true);
}
}
}
Comment
Best Answer
Answer by SolidSnake · Sep 26, 2012 at 02:45 PM
why are calling the update function from start? its done for you..
are you attaching this script to the object you are deactivating? if so you shouldnt because when you deactive an object you deactivate the script as well
Answer by jaskij · Sep 26, 2012 at 02:42 PM
If that script is on the object itself, then it won't work. The thing is, Update() is NOT called on inactive objects.
Answer by Daten · Sep 26, 2012 at 03:48 PM
You might want to try detecting the mouse Up
void Update()
{
if (Input.GetMouseButtonDown(1))
{
Sniper.SetActiveRecursively(false);
}
if (Input.GetMouseButtonUp(1))
{
Sniper.SetActiveRecursively(true);
}
}