- Home /
Scripting - Refering Pre-Made Objects
I'm stuck at my game and I need help....I looked around and I couldn't find answer to this anywhere. I know how I can modify for example a cube if I created it in script, but I have no idea how can I change it if I haven't created it in script, if I made it outside a script (like a map)... So my question is how can I modify a gameobject (in script) I created using unity without any scripting?
P.S. One more question, it's similar to last one. I have a cube in game, i have a new material. How can I change the cube's material to that new material? :) (script)
Answer by nsxdavid · Feb 25, 2013 at 11:13 PM
One way is by wiring up a reference to the object.
Create a public variable in a behavior script of a type that you want to reference. The simplest is a GameObject.
So like:
public GameObject thingIWantToChange;
Now if you select the object with the behavior you created that has this, you'll see a property called "Thing I Want To Change". You can then drag-n-drop any gameobject from the Hiearchy into the slot next to it. You've wired the two together!
Now you can manipulate thingIWantToChange all you want. Like thingIWantToChange.transform.position = new Vector3(10,10,10);
If you don't want to prewire, you have options like Find().
And to give you your second answer your looking for
thingIWantToCahnge.renderer.material.SetTexture( "_Texture", yourtexturehere);
Where yourTexture here can be anything. I suggest do antother call like he says
publix Texture yourtexturehere;
so you can set it from the inspector.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093405im_/https://answers.unity.com/themes/thub/images/avi.jpg)