- Home /
Same script not working on all objects
So let me simplify it and make it clear so you can understand how ridiculous this is.
a) I have a script called "talking" with which you can interact with an object and a GUI will appear.
b) I have two planes with different textures, both with the script "talking".
c) The planes are EXACTLY the same, next to each other.
d) One works(1) the other doesn't(2), meaning when I interact with (1) the GUI pops up but when I interact with (2) it doesn't.
e) When I copy paste the (1) and insert the texture of the (2) the (2) magically works!
f) When I save my work, exit Unity and then reopen the scene (2) doesn't work.
g) When I redo step e) it works until I exit Unity again and reopen the scene.
I have many exact planes in my scene with the "talking" script. Half of them weren't working. So I did step e) and nearly all of them worked even after I exited Unity. Some are still not working though. I tried to create a new plane from start instead of copy/paste but it still doesn't work!
Why is this happening? Why some work and others don't? Oh and get this - the remaining scripts that don't work, when I do step e) they work! So there's no invisible collider or anything invisible that doesn't make them work, because they do. But after I exit Unity and restart it they are NOT WORKING? So what the...is going on!?
Sorry for the long post but please help because this is hilarious and EXTREMELY annoying!... Thank you!
If you don't show any of your code it is very hard to say... $$anonymous$$aybe plane position in the world has something to do? $$anonymous$$aybe interaction time? Have you run the debugger to see if there is any exception thrown?
Yes the debugger says it's all fine...actually there's nothing wrong with the code since it works on the other planes. I have also tried changing the position of the planes but the results are the same. But here are both scripts: One is "talking" the other is "rayselect". I use the "rayselect" to recognize if an object has the "talking" script so I can interact with it.
"talking"
var hud : boolean = false;
var keydown : boolean = false;
var $$anonymous$$ySkin : GUISkin;
var subtitle : String = "(enter subtitle text)";
//var boxheight: int;
var boxsizeheight : int;
var boxsizewidth : int;
function Start () {
}
function Update () {
hud=false;
}
function OnGUI ()
{
if (hud==true && keydown==true)
{
GUI.skin = $$anonymous$$ySkin;
GUI.Box (Rect ((Screen.width-boxsizewidth)/2,(Screen.height-boxsizeheight)/2,boxsizewidth,boxsizeheight), subtitle);
} else {
keydown=false;
}
}
function OnLookEnter () {
hud=true;
keyhandle();
}
function keyhandle ()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)==true) {
if (!keydown){
keydown=true;
} else {
keydown=false;
}
}
}
"rayselect"
using UnityEngine;
using System.Collections;
public class rayselect : $$anonymous$$onoBehaviour {
public RaycastHit hit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0 ));
if (Physics.Raycast(ray, out hit, 3)){
if(hit.collider.gameObject.GetComponent<talking>() != null){
hit.collider.gameObject.GetComponent<talking>().OnLookEnter ();
}
}
}
}
P.S: Thank you guys for helping!
Answer by raimon.massanet · Oct 14, 2013 at 07:33 AM
I would say your problem has something to do with you re-setting the hud
variable in Update
. You are "activating" your talking script on OnGUI
, but deactivating it on Update
. So, maybe the problem is not that the script is not working, but you never get to see it working because it works only for a few milliseconds.
Why are you disabling hud in Update
? I think you could handle all UI logic in OnGUI
.
Your answer
Follow this Question
Related Questions
trouble adding script to clone object 1 Answer
getting the object that the running script is attached to 1 Answer
Function action applies to every object that have script with that function on it. Help please 1 Answer
Ammo Script gives "object reference not set to an instance of an object" 1 Answer
reseting rotation of objects? 0 Answers