- Home /
Change a value on a disabled gameobject
I want a script to change a value on a disabled gameobject (for example, xyz pos in rect transform). Is this possible? If yes, how do you do this?
You could just disable it's mesh renderer, so you can't see it, but it's still there. By doing it you can easily code it to move.
Answer by JonHollister · Dec 03, 2016 at 10:04 AM
You'll need a variable for the gameObject. Declare it at the top with the rest of your variables like this:
public GameObject targetObject;
You can either assign the proper gameObject via the inspector or get it via code. Once you have the proper gameObject assigned to that variable you would use the following:
targetObject.GetComponent<scriptName> ().changeThis = value;
Where "scriptName" is the name of the script attached to the gameObject and "changeThis" is the name of the variable in that script that you wish to change.
This works whether the gameObject is enabled or not.
Hope that helps!
Answer by Yoshinator2 · Dec 02, 2016 at 03:57 AM
This is possible. You would have to create a script to do this, I use the name LevelManager. Then you have to create an empty game object to attach the script to. The location of the object in the scene does not matter. Then, in the script, you would have to reference the script of the gameObject that you want to modify. Once you have done that, just get the position and change it. If you have any questions feel free to reply. Please accept the answer if this helped!
Your answer
![](https://koobas.hobune.stream/wayback/20220612101838im_/https://answers.unity.com/themes/thub/images/avi.jpg)