So im currently developing a game because im doing a formation to begin unity and coding in general and i want to use the gameobject in the script without using it in a public class cause its getting cloned
so its like:
cube 1 script:
if(col.gameObject.name == “Wallleft”) {
cube.transform.position = new Vector2(0,0)
// I want it like that
cube 1(cloned) script:
if(col.gameObject.name == “Wallleft”) {
cube(cloned).transform.position = new Vector2(0,0)
// Here i want now the cube cloned to have the same code but it changes automatically to the gameoject using the script.
i had the idea to make 3 scripts but it would be too dezorganized for me so if you have a solution like what i want or something like that, i’d appreciate it
not sure what “public class because its getting cloned” means
you can reference the gameobject you are on by just using gameobject,
i meant I dont want to use a public GameObject
why do you think it had to be public?
the gameobject array can be private so only visible to the inside of that instance of the class.
You also can child the other objects to the one this say runs on and the children dont arguably move only this does.
I’m pretty sure bugfinders already posted the solution for you. Just use:
gameObject.transform.position = new Vector2(0,0);
That specifically targets the gameobject to which the script belongs.
1 Like
You can use conditional statements so the functionality changes for the cloned cube.
For example:
If(col.gameObject.name=="WallLeft")
{
DoSomething();
}
else if (col.gameObject.name=="WallLeft(Cloned)")
{
DoSomethingElse();
}
Hope this fixes your issue .