- Home /
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?
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();
}
}
}
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. :-)
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
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.
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
Follow this Question
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