Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IHazABone · Oct 12, 2013 at 08:30 PM · javascriptif-statements

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. ");
                  }
             }
         }
     }
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ArkaneX · Oct 12, 2013 at 08:51 PM 0
Share

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.

avatar image IHazABone · Oct 12, 2013 at 08:57 PM 0
Share

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.

avatar image ArkaneX · Oct 12, 2013 at 09:07 PM 0
Share

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?

avatar image IHazABone · Oct 12, 2013 at 09:14 PM 0
Share

If I go near the object, it says "Button Pressed". Yeah, you're correct. So, how might I go about fixing it...?

avatar image ArkaneX · Oct 12, 2013 at 09:20 PM 0
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IHazABone · Oct 12, 2013 at 10:03 PM 0
Share

See, the problem with that, is it HAS to be in the trigger script. There is no possible work-around.

avatar image ArkaneX · Oct 12, 2013 at 10:08 PM 0
Share

Hmmm... $$anonymous$$ay I ask why? Because this might be crucial for answer.

avatar image IHazABone · Oct 12, 2013 at 10:19 PM 0
Share

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.

avatar image ArkaneX · Oct 12, 2013 at 10:21 PM 0
Share

Umm - do you mean problem is accidentally solved? ;)

avatar image IHazABone · Oct 12, 2013 at 10:25 PM 0
Share

Haha, yeah. Thanks for your help, though!:)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges