You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中访问并修改另一个脚本的Vector3属性NewMax的方法及CS0120错误解决方案

Fixing CS0120 Error When Accessing LeanRemapValue's NewMax Property

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:

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;
}

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's x won'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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 17:37:32