Unity中访问并修改另一个脚本的Vector3属性NewMax的方法及CS0120错误解决方案
Let's break down what's going wrong here and fix it step by step: that CS0120 error is telling you exactly the issue — NewMax is a non-static property of the LeanRemapValue class, so you can't access it directly using the class name (Lean.Common.LeanRemapValue). Non-static members belong to individual instances of a class, not the class itself.
Here are the proper ways to resolve this, including the GetComponent approach you asked about:
Method 1: Use GetComponent<LeanRemapValue>() to Get an Instance (Recommended)
This is the standard Unity way to interact with components on GameObjects. Here are a few variations depending on where your LeanRemapValue component is located:
Option 1: Get the Component from the Same GameObject
If the LeanRemapValue is attached to the same GameObject as your script, use this code:
public void voltageFlowCheck() //meter { // Grab the LeanRemapValue component instance from the current GameObject Lean.Common.LeanRemapValue remapInstance = GetComponent<Lean.Common.LeanRemapValue>(); // Always check for null to avoid runtime crashes if (remapInstance != null) { // Important: Vector3 is a value type — you can't modify its x directly through the property // First get a copy of the NewMax value, modify it, then assign it back Vector3 updatedNewMax = remapInstance.NewMax; updatedNewMax.x = 2; remapInstance.NewMax = updatedNewMax; } Amount_of_Voltage_Passing_out = Lean.Common.LeanFormatString.Qapp_value; }
Option 2: Get the Component from a Specific GameObject
If the LeanRemapValue is on a different GameObject, you can either find it by name or reference it directly in the Inspector:
Find by GameObject Name:
public void voltageFlowCheck() //meter { // Replace "YourRemapObject" with the actual name of your target GameObject GameObject targetObject = GameObject.Find("YourRemapObject"); if (targetObject != null) { Lean.Common.LeanRemapValue remapInstance = targetObject.GetComponent<Lean.Common.LeanRemapValue>(); if (remapInstance != null) { Vector3 updatedNewMax = remapInstance.NewMax; updatedNewMax.x = 2; remapInstance.NewMax = updatedNewMax; } } Amount_of_Voltage_Passing_out = Lean.Common.LeanFormatString.Qapp_value; }
Assign via Inspector (Most Reliable):
Declare a public reference at the top of your script, then drag the GameObject with LeanRemapValue into the field in the Unity Inspector:
// Add this at the top of your script (outside any method) public Lean.Common.LeanRemapValue remapValueReference; public void voltageFlowCheck() //meter { if (remapValueReference != null) { Vector3 updatedNewMax = remapValueReference.NewMax; updatedNewMax.x = 2; remapValueReference.NewMax = updatedNewMax; } Amount_of_Voltage_Passing_out = Lean.Common.LeanFormatString.Qapp_value; }
Method 2: Make NewMax Static (Not Recommended Unless Necessary)
If you really need to access NewMax using the class name, you can convert it to a static property. However, this will make all instances of LeanRemapValue share the same NewMax value, which might not fit your use case.
Modify the LeanRemapValue script like this:
using UnityEngine; using UnityEngine.Events; namespace Lean.Common { [HelpURL(LeanHelper.PlusHelpUrlPrefix + "LeanRemapValue")] [AddComponentMenu(LeanHelper.ComponentPathPrefix + "Remap Value")] public class LeanRemapValue : MonoBehaviour { // Keep your existing code for other properties/events... // Change the field and property to static private static Vector3 newMax = Vector3.one; public static Vector3 NewMax { set { newMax = value; } get { return newMax; } } } }
With this change, your original code will work, but be aware of the global state side effect.
Key Notes
- Value Type Quirk: Vector3 is a value type, so when you get
remapInstance.NewMax, you're getting a copy. Modifying the copy'sxwon't affect the original property — you have to assign the modified copy back. - Null Checks: Always verify that component references aren't null to prevent runtime exceptions.
内容的提问来源于stack exchange,提问作者Savad

