如何从另一个脚本访问脚本的值并统一修改#
how to Access value of a script from another script and modify in unity#
您好想在另一个脚本中访问 public 向量值 NewMax 并修改 x 值
using UnityEngine;
using UnityEngine.Events;
namespace Lean.Common
{
/// <summary>This component allows you to convert 1, 2, or 3 values from one range to another. For example, an angle in the range of -90..90 could be converted to 0..1. This is done by calling one of the <b>SetX/Y/Z</b> methods, and then sending it out using the <b>OnValueX/Y/Z</b> events.</summary>
[HelpURL(LeanHelper.PlusHelpUrlPrefix + "LeanRemapValue")]
[AddComponentMenu(LeanHelper.ComponentPathPrefix + "Remap Value")]
public class LeanRemapValue : MonoBehaviour
{
[System.Serializable] public class FloatEvent : UnityEvent<float> {}
[System.Serializable] public class Vector2Event : UnityEvent<Vector2> {}
[System.Serializable] public class Vector3Event : UnityEvent<Vector3> {}
/// <summary>The range of the input values.</summary>
public Vector3 OldMin { set { oldMin = value; } get { return oldMin; } } [SerializeField] private Vector3 oldMin;
/// <summary>The range of the input values.</summary>
public Vector3 OldMax { set { oldMax = value; } get { return oldMax; } } [SerializeField] private Vector3 oldMax = Vector3.one;
/// <summary>The range of the output values.</summary>
public Vector3 NewMin { set { newMin = value; } get { return newMin; } } [SerializeField] private Vector3 newMin;
/// <summary>The range of the output values.</summary>
public Vector3 NewMax { set { newMax = value; } get { return newMax; } } [SerializeField] private Vector3 newMax = Vector3.one;
}
}
当我从另一个脚本尝试这个时
public void voltageFlowCheck() //meter
{ Lean.Common.LeanRemapValue.NewMax.x=2;
Amount_of_Voltage_Passing_out = Lean.Common.LeanFormatString.Qapp_value;
}
我收到以下错误:-
Vector3 Lean.Common.LeanRemapValue.NewMax { get; set; } The range of
the output values.
An object reference is required for the non-static field, method, or
property 'LeanRemapValue.NewMax' [Assembly-CSharp]csharp(CS0120)
我该如何解决这个问题并获得价值?有什么方法可以使用 gameobject.GetComonent<>()
来调用它
首先,您必须获得 LeanRemapValue class 附加到的 GameObject,然后使用
`.GetComponent().NewsMax 你得到值并可以修改它。
您可以在编辑器中或使用GameObject.Find()
方法获取GameObject。
如果脚本继承自 MonoBehaviour,则它必须附加到 GameObject,然后脚本充当组件。 .
您好想在另一个脚本中访问 public 向量值 NewMax 并修改 x 值
using UnityEngine;
using UnityEngine.Events;
namespace Lean.Common
{
/// <summary>This component allows you to convert 1, 2, or 3 values from one range to another. For example, an angle in the range of -90..90 could be converted to 0..1. This is done by calling one of the <b>SetX/Y/Z</b> methods, and then sending it out using the <b>OnValueX/Y/Z</b> events.</summary>
[HelpURL(LeanHelper.PlusHelpUrlPrefix + "LeanRemapValue")]
[AddComponentMenu(LeanHelper.ComponentPathPrefix + "Remap Value")]
public class LeanRemapValue : MonoBehaviour
{
[System.Serializable] public class FloatEvent : UnityEvent<float> {}
[System.Serializable] public class Vector2Event : UnityEvent<Vector2> {}
[System.Serializable] public class Vector3Event : UnityEvent<Vector3> {}
/// <summary>The range of the input values.</summary>
public Vector3 OldMin { set { oldMin = value; } get { return oldMin; } } [SerializeField] private Vector3 oldMin;
/// <summary>The range of the input values.</summary>
public Vector3 OldMax { set { oldMax = value; } get { return oldMax; } } [SerializeField] private Vector3 oldMax = Vector3.one;
/// <summary>The range of the output values.</summary>
public Vector3 NewMin { set { newMin = value; } get { return newMin; } } [SerializeField] private Vector3 newMin;
/// <summary>The range of the output values.</summary>
public Vector3 NewMax { set { newMax = value; } get { return newMax; } } [SerializeField] private Vector3 newMax = Vector3.one;
}
}
当我从另一个脚本尝试这个时
public void voltageFlowCheck() //meter
{ Lean.Common.LeanRemapValue.NewMax.x=2;
Amount_of_Voltage_Passing_out = Lean.Common.LeanFormatString.Qapp_value;
}
我收到以下错误:-
Vector3 Lean.Common.LeanRemapValue.NewMax { get; set; } The range of the output values.
An object reference is required for the non-static field, method, or property 'LeanRemapValue.NewMax' [Assembly-CSharp]csharp(CS0120)
我该如何解决这个问题并获得价值?有什么方法可以使用 gameobject.GetComonent<>()
来调用它首先,您必须获得 LeanRemapValue class 附加到的 GameObject,然后使用 `.GetComponent().NewsMax 你得到值并可以修改它。
您可以在编辑器中或使用GameObject.Find()
方法获取GameObject。
如果脚本继承自 MonoBehaviour,则它必须附加到 GameObject,然后脚本充当组件。 .