- Home /
How to deactivate a script temporarly on another object when clicking a GUI ?
Hi, I've got 2 cameras : one (called ObjectCamera) is looking at an object (with Camera_Orbit when clicking), sending the view in a RenderTexture, the second (MainCamera) sees GUI features and the RenderTexture. When clicking on the GUI and moveing the mouse, the ObjectCamera turns around the object.
How could I disable the rotation when clicking on the GUI objects ?
Here's the code I tested, but during the test it says
UnityEngine.GameObject.GetComponent (System.String type) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:34) RotationDeactivate.OnMouseDown () (at Assets/Demo Fly/Scripts/RotationDeactivate.js:2) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32) function OnMouseDown(){ GameObject.Find("ObjectCamera").GetComponent("MouseOrbit_onclick").enabled = false; } Any idea ? Thanks a lot.NullReferenceException
Answer by hamstar · Oct 01, 2013 at 11:39 AM
It either can't find the GameObject named "ObjectCamera", or the camera does not have a script attached to it called "MouseOrbit_onclick". Check that the camera name and script name match what you wrote exactly.
Also, just check you aren't mixing up the object's name with it's tag.
Alternative if you want to give ObjectCamera a tag and use that:
GameObject.FindWithTag("ObjectCamera").GetComponent("MouseOrbit_onclick").enabled = false;
[edit]
In response to your 2nd issue (see below).
I'm not sure this is the best solution, but might be worth a try. Create an empty object and give it a GUITexture component. Position and size the gui texture to fit your "window" (sofa view), then give the GUITexture a transparent texture.
Then add a new script like this to the new object:
public var objectCamera : Transform;
private var orbitZoomScript : MouseOrbitZoom;
function Start () {
orbitZoomScript = objectCamera.GetComponent(MouseOrbitZoom);
}
function OnMouseEnter() {
orbitZoomScript.enabled = true;
}
function OnMouseExit() {
orbitZoomScript.enabled = false;
}
Yay thanks, i renamed my camera and it works. But the feature "$$anonymous$$ouseOrbit_onclick" stays deactivated after that... I tried to add an On$$anonymous$$ouseUp function but now it still gets activated.
function On$$anonymous$$ouseUp(){
GameObject.Find("ObjectCamera").GetComponent("$$anonymous$$ouseOrbitZoom").enabled = true;
}
I tried with "if" and "else" but doesn't work.
function On$$anonymous$$ouseDown () {
var camera : $$anonymous$$ouseOrbit_onclick = gameObject.Find("ObjectCamera").GetComponent($$anonymous$$ouseOrbitZoom);
if (Input.Get$$anonymous$$ouseButton(0)){
camera.enabled = false;
}
}
What is the reverse function to use there ? Thanks a lot :)
That's almost there. But you need GameObject.Find
. With gameObject.Find
you are just searching on the gameObject the script it attached to.
function On$$anonymous$$ouseDown() {
GameObject.Find("ObjectCamera").GetComponent($$anonymous$$ouseOrbitZoom).enabled = false;
}
function On$$anonymous$$ouseUp() {
GameObject.Find("ObjectCamera").GetComponent($$anonymous$$ouseOrbitZoom).enabled = true;
}
Thanks a lot ! That works, i just have to put that script on the good Gui now...
thanks again hamstar :)
Answer by Teapot-C · Oct 01, 2013 at 01:29 PM
Erf, I finally have another issue with that script...
When added onto a GUITexture, it works fine. But when added onto a slider, it doesn't ; sliding the thumb makes the camera rotate.
I've tried to change the depth but no way.
Where do I need to put this script to block the rotation when using the slider too ?
A screenshot of what you're doing might be useful. Perhaps you can use GUIUtility.hotControl in your $$anonymous$$ouseOrbitZoom scripts. Enclose all your logic in an if like:
if(GUIUtility.hotControl == 0) {
// check user input and do Orbit/Zoom
}
Alright here is a screenshot.
The camera turns around the sofa when left click & drag. Also the camera zooms in/out when clic&drag right button.
With the script explained in this post, when clicking on the dark area or the tumbnails, the camera doesn't move.
The white square is the slider thumb and shows which material is selected and applied on the sofa (can be changed by dragging horizontally, as seen on clipboard2):
I've modified my script so that it takes care of both left and right buttons :
function On$$anonymous$$ouseOver () {
if (Input.Get$$anonymous$$ouseButtonDown(1)){
GameObject.Find("ObjectCamera").GetComponent("$$anonymous$$ouseOrbitZoom").enabled = false;
}
if (Input.Get$$anonymous$$ouseButtonDown(0)){
GameObject.Find("ObjectCamera").GetComponent("$$anonymous$$ouseOrbitZoom").enabled = false;
}
if (Input.Get$$anonymous$$ouseButtonUp(1)){
GameObject.Find("ObjectCamera").GetComponent("$$anonymous$$ouseOrbitZoom").enabled = true;
}
if (Input.Get$$anonymous$$ouseButtonUp(0)){
GameObject.Find("ObjectCamera").GetComponent("$$anonymous$$ouseOrbitZoom").enabled = true;
}
}
I've added this script to each tumbnail, the dark area and the slider. But dragging the slider still rotates the camera around the sofa...
Thanks for helping :)
Hm i've looked for that hotcontrol stuff, but i think I can't put my whole $$anonymous$$ouseOrbitZoom scrpit in there... I have some function Update, LateUpdate and a static ClampAngle function. What should I put in the "if(GUIUtility.hotControl == 0) {" section ?
Thank you a lot hamstar, now it works perfectly ! I tried the opposite, i mean setting the inversed script on an invisible GUItexture over the slider, but it doesn't work (don't really know why, probably a Z coord value). But I'll continue to use that script this way. Thanks again !
Your answer
Follow this Question
Related Questions
Mouse Look Script Pause 3 Answers
A GUI error in script??? HELP,Please!! 1 Answer
How to Hide the GUI when time.scale = 1 again 1 Answer
Transitions When Pausing? 1 Answer
Pause Problem 1 Answer