r/UnityHelp • • Jan 18 '22

Does anyone know which section of this code is controlling the physical game object?

I'm creating a script that will change out the material of an object for a different one when prompted. So far the closest I've gotten is this, but this code works ONLY when the script itself is attached to that physical object. The issue with this is that I need this to be remote and not attached so I can edit the materials of multiple different objects from one single script. My question is what part of this code decides what physical object is affected and a follow up question what would I have to do to allow this to be made without having to put the script on the game object? Thank you to anyone who gives their time to this!

3 Upvotes

16 comments sorted by

1

u/Bonejob Code Guru Jan 18 '22

None of that controls the physics of the object. Physics is normally and associated 2d or 3d RigidBody, Collider or Both.

https://docs.unity3d.com/Manual/PhysicsSection.html

1

u/ActNo5151 Jan 18 '22

Oh I don't mean to control the physics of the object, just "the object" if that makes sense. So like right now the code is set up to change the material of whatever object it is attached to, but what I'm wondering is how I would do that without having to attach the script to the "cube" for example. Thank you so much though, sorry I made it confusing.

0

u/Bonejob Code Guru Jan 18 '22

So you want to change the physic material in code? The physic material is the portion of the object that controls it's physics properties.

https://docs.unity3d.com/Manual/class-PhysicMaterial.html

https://docs.unity3d.com/ScriptReference/PhysicMaterial.html

Do I understand correctly?

1

u/ActNo5151 Jan 21 '22

I assume no, what I need is to just change the material that is on a 3d object in my scene in Realtime using scripts

1

u/Bonejob Code Guru Jan 23 '22

What do you want to do to the object? To change an object's properties you use the GetComponent function to return a reference to the object for the object property you want to interact with. If you want to change the location of the object, you use the Transform component. So what property do you want to do to the object?

1

u/Bonejob Code Guru Jan 23 '22

Why the hell was this downvoted, I am trying to find out what question I am answering? I do the best I can...

1

u/lavatasche Jan 18 '22

You need references to the objects as GameObject. If you call GetComponent right now without a reference its like saying, get me the renderer of the GameObject this script is attached to.

You could make a list of GameObjects and have acces to their renderes this way.

1

u/ActNo5151 Jan 19 '22

I'm not too familiar with the concept but what I mainly was looking for was using Getgameobject to change the object's material. All I need to know is where to put the gameobject variable in that code so that can recognize the object that I want, would you know where that goes if it would work at all?

1

u/lavatasche Jan 19 '22

it depends. Do you know what GameObjects you want to change materials on pre runtime? Then you could just make the list of GameObjects public and add them manually in the editor.

Like you make an empty Object in the scene that just holds the script.

Otherwise all the GameObjects that you want to change the materials on could have a reference in their script to this script and add themselves to the list, so this script can manage their materials

1

u/ActNo5151 Jan 20 '22

I do know what gameobjects I would want to change pre runtime. I can create the gameobjects, but I'm not sure where to put the variables so that it will work from a remote game object as you said.

1

u/lavatasche Jan 20 '22

Just as you have a public Material[] materials; in your code, you can put a public GameObject[] gameObjects; in your code.

Then you create all the GameObjects pre runtime and drag them manually in that array. You can see that array on the GameObject that holds this script in the editor because that field is public.

(There are also other ways to solve this. )

1

u/ActNo5151 Jan 21 '22

Yeah that's what I thought should work, but I don't know where to put the "gameObject" variable. Like I've already initialized the gameobject array but where would I put the gameObject variable so that the code actually recognizes it as the gameobject that will change it's materials?

1

u/lavatasche Jan 21 '22

You can do that in the editor. You dont have to initialise your array. Just google unity array editor.

1

u/ActNo5151 Jan 21 '22

I don't think I'm understanding you correctly or something, I've already put in the code "public GameObject[] gameObjects;" but that doesn't do anything on it's own unless you put the variable that it makes somewhere else. That's basically what I'm asking, where would I put the variable in the code that I had already provided above if it even works that way. Like (I've already tried this, not the right spot I assume): "rend = GetComponent<gameObjects>();". Like where in the code will I put that so that I won't have to attach the script to the same object that I want to change the material of and so I can change it remotely? That's all I need to know, is just where to put the gameObjects variable to where it will do what I need it to do.

1

u/lavatasche Jan 22 '22

What you get then is an array of gameObjects. You would have to iterate over the array and change everyones material individually if prompted.

I recommend looking up what arrays are and how to use them if thats the problem.

1

u/lavatasche Jan 22 '22

Also maybe you should put the statement at line 33 into the if clause, because right now you are assigning the new material to the gameObject every Frame which may hinder performance.