- Home /
Object not moving on KeyPress?
If I go near the object that this script is attached to and press 'e', nothing happens. I do have a MonoBehaviour
and object assigned in the inspector.
var script : MonoBehaviour;
var object : GameObject;
private var arrayIndex = 0;
private var arr : MonoBehaviour[];
function Awake() {
if (script == null)print ("Script value is not assigned, you will get a NullRefExc if you continue");
arr = GameObject.FindObjectsOfType(script.GetType()) as MonoBehaviour[];
if (arr == null || arr.Length == 0)
print ("No objects of type " + script.GetType() + " was found");
}
function Update() {
if(Vector3.Distance(object.transform.localPosition, this.transform.localPosition) < 2) {
if(Input.GetKeyDown(KeyCode.E)) {
if (script.GetType() == typeof TransformObject) {
var obj = script as TransformObject;
obj.condition = true;
Debug.Log("Button pressed. ");
}
}
}
}
I've just did a quick setup using your code and it works. I guess the problem might be the first if
statement, where you calculate distance between local positions. This will only work if both objects have no parent or if they have the same parent.
As far as I know, they don't have parents. I commented the if statement and it's brace and it still won't work.
So for me it seems script is not of type TransformObject. Could you temporarily comment lines 19-21 and 23 from your above script as well?
If I go near the object, it says "Button Pressed". Yeah, you're correct. So, how might I go about fixing it...?
It depends what is your goal :) It's hard to guess it looking at your code... Could you describe shortly what you want to achieve?
Answer by ArkaneX · Oct 12, 2013 at 09:58 PM
After exchanging a few comments...
If you need this to work using buttons, then in my opinion it is better to move key press logic to TransformObject
script. Apart from your current code in this script, you have to include the following:
var activator : Transform; // the object I named Activator in one of the comments
and in the Update:
if(Vector3.Distance(activator.position, transform.position) < 2 && Input.GetKeyDown(KeyCode.E)) {
condition = true;
}
Of course you can then remove unnecessary parts of your first script, or even delete it, if Awake
part is not crucial for you.
See, the problem with that, is it HAS to be in the trigger script. There is no possible work-around.
Hmmm... $$anonymous$$ay I ask why? Because this might be crucial for answer.
Ah, sorry, it seems that the guy who organized the scene assets accidentally put two of all the scripts in and it was causing interference as to what was being referenced.
Your answer
Follow this Question
Related Questions
Input.GetMouseButtonDown(0) running through my if statments too quickly 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
EXTREMELY basic scripting question. 2 Answers
really strange thing with "if" statment not working well 1 Answer
If...else statement with errors 1 Answer