Setting Component Value

Hi

I have been racking my brain for the past few days, and every solution I try seems to fail,so i;m wondering if anyone can please help me. I’m quite new to Unity.

I have a script which is attached to a cube, and the script allows you to change the pivot point of an object, and you can set the x y z pivot under the inspector when the cube is selected.

A screenshot attached shows the code and the “RotateAroundPivot” options in the inspector.

What I would like to do is from a totally different C# script, change the x y or z value for that cube, so rather than being x: 0 it would be x:35

So, is it possible to place some code within

public void SetCubePivot() { }

that would alter those pivot values for the cube

Hopefully that makes sense :slight_smile:

Thanks very much, it would be a real help if someone can give me some advice

If I understand you correctly, you would simply get a reference to the RotateAroundPivot component (for example using GetComponent), and then update its Pivot property, e.g.:

GetComponent<RotateAroundPivot>().Pivot.x = 35;

HTH,

  • Joe

Thanks Joe, that kind of makes sense. My question is, using your example, how would I tell the script which object to change the Pivot X value on?

So within a script that’s not attached to the cube, how would I tell GetComponent() to perform this action on a specific object (ie: cube)

Thanks for help so far Joe

Well, just

public GameObject cube;

in the script, then drag and drop the cube on the inspector. The “public” makes that.

and then in the script where you want to make it rotate:

cube.GetComponent<RotateAroundPivot>().Pivot.x = 35;
1 Like

Thanks very much,works great now