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 Darth Lordi · Aug 13, 2013 at 12:49 PM · prefabupdateselectfunction update

Update running multiple time per componant

I have made a prefab of a cube which I want to use for puzzles of various sizes. I have managed to get a script together which selects an object at a time, but when I try to move one via the Update function, it seems to run multiple times - once for each prefabbed cube I have added.

 #pragma strict
 
 static var myObject : GameObject;
 static var initialColor : Color;
 
 function Start () {
 }
 
 function OnMouseDown () {
 if (!myObject){
         myObject = gameObject;
         initialColor = myObject.renderer.material.color;
         myObject.renderer.material.color =     Color.green;}
 
 else if (myObject){
         myObject.renderer.material.color =     initialColor;
         myObject = null;   
         myObject = gameObject;        
         myObject.renderer.material.color =     Color.green;}
 }
 
 function Update() {
 if(myObject && Input.GetKeyUp(KeyCode.W)){
 
         myObject.transform.Translate( 0,1,0);
       }
 
 if(myObject && Input.GetKeyUp(KeyCode.S)){
 
         myObject.transform.Translate( 0,-1,0);
 }
         
  if(myObject && Input.GetKeyUp(KeyCode.A)){
 
         myObject.transform.Translate( -1,0,0);    
 }
         
 if(myObject && Input.GetKeyUp(KeyCode.D)){
 
         myObject.transform.Translate( 1,0,0);
 }   
 
 if(myObject && Input.GetKeyUp(KeyCode.E)){
 
         myObject.transform.Rotate( 0,0,-90);
 } 
 
 if(myObject && Input.GetKeyUp(KeyCode.Q)){
 
         myObject.transform.Rotate( 0,0,90);
 } 
 }

Is there a way to split the code up so Update only runs on the object selected?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by Linus · Aug 13, 2013 at 01:16 PM

You should have a script attached to an empty game object, for input controls I tend to place them on the main camera.

In that script you should have all the Input.GetkeyDown and maybe the mouse click events, see next section.

Then you can do it two ways, either have a script on each object that calls home to the game manager script, to say they got selected. Or as I tend to do, have the game manager script let the objects know that they got selected.

Here is one of my input manager scripts with some modification in regards to your issue.

 #pragma strict
 private var GM : gameManager;
 var playerSelected    : playerAI;
 var actionPanel : Transform;
 var mCam : Camera;
 var myObject : Transform; //selectedObject could be a better variable name, since you are mostly using the transform of the GO, might as well make it a transform type
 
 
 
 function Start () 
 {
     GM        = GameObject.Find('GameManager').GetComponent(gameManager);
 
     actionPanel = GameObject.Find('ActionPanel').GetComponent(Transform);
     playerSelected = null;
     
     
 }
 
 function LateUpdate () 
 {
     if(Input.GetMouseButtonDown(0)){
         OnLeftMouseDown();
     }
     
     if(Input.GetMouseButtonDown(1)){
         Debug.Log('mouse at:'+Input.mousePosition);
     
     
         playerSelected = null;
 myObject = null; //deselect on right click
     }
 
 if(myObject != null && Input.GetKeyUp(KeyCode.D)){
  
 myObject.Translate( 1,0,0);
 } 
 
     
 }
 
 function OnLeftMouseDown()
 {
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
         
     if (Physics.Raycast (ray, hit, 600)) {
         Debug.Log('Click '+hit.transform.name);
 
         if(playerSelected){
             playerSelected.NewOrder(hit);    
         }
             
         if(hit.transform.tag == 'Player'){
             playerSelected = hit.transform.GetComponent(playerAI);
            }
         
         if(hit.transform.name == 'some puzzle object'){
             Debug.Log('Clicked '+hit.transform.name);
             myObject = hit.transform;
 //if there is a script named someScript on that object, the function GotSelected() will run
 
 myObject.GetComponent(someScript).GotSelected();
 
             
            }        
              
     }    
 }
 
  
Comment
Add comment · Show 2 · 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 Darth Lordi · Aug 13, 2013 at 02:31 PM 0
Share

Thanks for this. With a little bit of tweaking I was able to get this to function. I wasn't sure about raycasting which is why I was trying to use On$$anonymous$$ouseDown to select. After many hours of Googling and tutorials this is the first time I've seen someone mention running the Start and Update script on an object like a camera. It makes so much more sense now. :-)

avatar image Linus · Aug 13, 2013 at 02:52 PM 0
Share

Glad you find it useful. And glad you are able to $$anonymous$$ch yourself as well by searching, reading and making modifications. Only way to learn

avatar image
0

Answer by Joyrider · Aug 13, 2013 at 12:54 PM

you should not declare your variable myObject as static, unless you want it to be shared by all scripts, on all objects.

In your case, every object with this script, affect one and the same object.

If you remove the static keyword, every script will have his own myObject variable. And that variable can than be different on each object you have the script on.

Comment
Add comment · Show 1 · 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 Darth Lordi · Aug 13, 2013 at 02:31 PM 0
Share

I can see what you are saying here. The problem I had was that I couldn't get the Selection (On$$anonymous$$ouseDown) section to work until I set it to Static. That's what broke the rest of the script.

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

17 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 avatar image

Related Questions

Mouse Hold Down 2 Answers

Changing prefab doesn't update instances (in scene aswell as in other prefabs).. 3 Answers

Keep constant number of prefabs 1 Answer

How do I import packages with Prefabs to Update my package automatically? 1 Answer

How do I Instantiate a prefab to touch and still update a score? 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