- Home /
how can i target a nother cube from a script
i made a script where by clicking on a cube/button it moves how do i make it so that when i click it it moves a nother cube not the cube that i clickd pls help here is the script:
var speed : float = 0.2;
var timer : float = 0;
var player = this;
function Update (){
if(timer >= 1)
//here is the line wherei would like to move a nother cube
{
this.transform.position.x += this.speed;
}
}
function OnMouseDown () {
timer++;
}
function OnMouseUp () {
timer--;
}
i would like i tthat when it says:
this.transform.position.x += this.speed;
it would move a nother cube pls help tnx
add a new variable :
var anothercube :GameObject;
then your script should be like this :
anothercube.transform.position.x += this.speed;
anothercube is the cube that you wana move it ! add your cube in editor to the anothercube variable then play ! or you just find it in start() function like this :
anothercube = GameObject.Find('your cube name');
wow thank you it works oh my gosh im so greatful thank you this unity furom thing realy pays of thank you again. i was trying to solve this problome for the past day (im a biginer) thank you.
Please accept @Qasem2014 's answer if it solved your problem. Thanks!
link text I convert my comment to answer , so you can close the question if your problem solved
Answer by banana111 · Nov 05, 2018 at 03:37 PM
Yo your such a noob.. omg ... you need to declare the object as a var. public Gameobject something; since it's a public Gameobject you can set the Gamobject variable to be equal to your desired object in the inspector. In your code change the this. to something. something being your new Game Object variable you just created. ALso learn how to code c# my response was in c# who still uses java script wtf noob.
Your answer
Follow this Question
Related Questions
How To Drag any Cube Using Mouse Click and Moved Only 1 Unit?? 2 Answers
does not work check if dead 1 Answer
Help around an approach to a "Virtual Motion Table" in Unity 0 Answers
Instantiated prefab position 5 Answers
Setting Position of Spawned Prefab 2 Answers