更改线宽出错,对象引用未设置为对象的实例
Change line width getting error, Object reference not set to an instance of an object
我希望能够更改线宽,但我似乎 运行 遇到了一些问题。我设置它的方式是我有 6 种不同的线宽,当用户点击宽度时,下次他们画线时应该反映出来。目前我收到错误:
"NullReferenceException: 对象引用未设置到对象的实例 BrushWidth.SetThickness (Int32 _lineNum) (at Assets/_Scripts/BrushWidth.cs:46) BrushWidth.Start () (at Assets/_Scripts/BrushWidth.cs:30)”
这是指改变线比例的线,lineConfig.Scale = 0.35f ;
我在这里错过了什么?我以为我用 public LineConfig lineConfig 做了我的实例引用;感谢您的帮助!
using UnityEngine ;
using System.Collections ;
using UnityEngine.UI ;
using System ;
using PaintCraft.Tools ;
public class BrushWidth : MonoBehaviour
{
public Button btn1, btn2, btn3, btn4, btn5, btn6 ;
public float brushSize ;
public Text brushNameTextBox ;
public String brushName ;
public LineConfig lineConfig ;
void Awake ()
{
lineConfig = gameObject.GetComponent<LineConfig> () ;
btn1.onClick.AddListener (() => SetThickness (1)) ;
btn2.onClick.AddListener (() => SetThickness (2)) ;
btn3.onClick.AddListener (() => SetThickness (3)) ;
btn4.onClick.AddListener (() => SetThickness (4)) ;
btn5.onClick.AddListener (() => SetThickness (5)) ;
btn6.onClick.AddListener (() => SetThickness (6)) ;
}
void Start ()
{
// Set Starting Thickness
SetThickness (3) ;
}
void SetThickness (int _lineNum)
{
switch (_lineNum)
{
case 1:
lineConfig.Scale = 0.1f ;
brushName = "Thin " ;
break ;
case 2:
lineConfig.Scale = 0.2f ;
brushName = "Light " ;
break ;
case 3:
lineConfig.Scale = 0.35f ;
brushName = "Regular " ;
break ;
case 4:
lineConfig.Scale = 0.5f ;
brushName = "Medium " ;
break ;
case 5:
lineConfig.Scale = 0.75f ;
brushName = "Thick " ;
break ;
case 6:
lineConfig.Scale = 1.0f ;
brushName = "Heavy " ;
break ;
default:
break ;
}
brushNameTextBox.text = brushName ;
}
}
您的问题是您没有将 LineConfig
组件附加到您的脚本所附加的对象。因此,GetComponent
returns 无效。要解决此问题,请将 LineComponent
添加到您的对象,或简单地更改:
lineConfig = gameObject.GetComponent<LineConfig> () ;
至:
lineConfig = gameObject.AddComponent<LineConfig>() ;
或者,如果您想确保已经附加了 LineConfig
,请将 [RequireComponent (typeof (LineConfig))]
添加到 class 的开头,高于 public class BrushWidth : MonoBehaviour
。
所以,你的 class 应该是这样的:
using UnityEngine ;
using System.Collections ;
using UnityEngine.UI ;
using System ;
using PaintCraft.Tools ;
public class BrushWidth : MonoBehaviour
{
public Button btn1, btn2, btn3, btn4, btn5, btn6 ;
public float brushSize ;
public Text brushNameTextBox ;
public String brushName ;
public LineConfig lineConfig ;
void Awake ()
{
lineConfig = gameObject.AddComponent<LineConfig> () ;
btn1.onClick.AddListener (() => SetThickness (1)) ;
btn2.onClick.AddListener (() => SetThickness (2)) ;
btn3.onClick.AddListener (() => SetThickness (3)) ;
btn4.onClick.AddListener (() => SetThickness (4)) ;
btn5.onClick.AddListener (() => SetThickness (5)) ;
btn6.onClick.AddListener (() => SetThickness (6)) ;
}
void Start ()
{
// Set Starting Thickness
SetThickness (3) ;
}
void SetThickness (int _lineNum)
{
switch (_lineNum)
{
case 1:
lineConfig.Scale = 0.1f ;
brushName = "Thin " ;
break ;
case 2:
lineConfig.Scale = 0.2f ;
brushName = "Light " ;
break ;
case 3:
lineConfig.Scale = 0.35f ;
brushName = "Regular " ;
break ;
case 4:
lineConfig.Scale = 0.5f ;
brushName = "Medium " ;
break ;
case 5:
lineConfig.Scale = 0.75f ;
brushName = "Thick " ;
break ;
case 6:
lineConfig.Scale = 1.0f ;
brushName = "Heavy " ;
break ;
default:
break ;
}
brushNameTextBox.text = brushName ;
}
}
希望对您有所帮助 :)
我希望能够更改线宽,但我似乎 运行 遇到了一些问题。我设置它的方式是我有 6 种不同的线宽,当用户点击宽度时,下次他们画线时应该反映出来。目前我收到错误:
"NullReferenceException: 对象引用未设置到对象的实例 BrushWidth.SetThickness (Int32 _lineNum) (at Assets/_Scripts/BrushWidth.cs:46) BrushWidth.Start () (at Assets/_Scripts/BrushWidth.cs:30)”
这是指改变线比例的线,lineConfig.Scale = 0.35f ;
我在这里错过了什么?我以为我用 public LineConfig lineConfig 做了我的实例引用;感谢您的帮助!
using UnityEngine ;
using System.Collections ;
using UnityEngine.UI ;
using System ;
using PaintCraft.Tools ;
public class BrushWidth : MonoBehaviour
{
public Button btn1, btn2, btn3, btn4, btn5, btn6 ;
public float brushSize ;
public Text brushNameTextBox ;
public String brushName ;
public LineConfig lineConfig ;
void Awake ()
{
lineConfig = gameObject.GetComponent<LineConfig> () ;
btn1.onClick.AddListener (() => SetThickness (1)) ;
btn2.onClick.AddListener (() => SetThickness (2)) ;
btn3.onClick.AddListener (() => SetThickness (3)) ;
btn4.onClick.AddListener (() => SetThickness (4)) ;
btn5.onClick.AddListener (() => SetThickness (5)) ;
btn6.onClick.AddListener (() => SetThickness (6)) ;
}
void Start ()
{
// Set Starting Thickness
SetThickness (3) ;
}
void SetThickness (int _lineNum)
{
switch (_lineNum)
{
case 1:
lineConfig.Scale = 0.1f ;
brushName = "Thin " ;
break ;
case 2:
lineConfig.Scale = 0.2f ;
brushName = "Light " ;
break ;
case 3:
lineConfig.Scale = 0.35f ;
brushName = "Regular " ;
break ;
case 4:
lineConfig.Scale = 0.5f ;
brushName = "Medium " ;
break ;
case 5:
lineConfig.Scale = 0.75f ;
brushName = "Thick " ;
break ;
case 6:
lineConfig.Scale = 1.0f ;
brushName = "Heavy " ;
break ;
default:
break ;
}
brushNameTextBox.text = brushName ;
}
}
您的问题是您没有将 LineConfig
组件附加到您的脚本所附加的对象。因此,GetComponent
returns 无效。要解决此问题,请将 LineComponent
添加到您的对象,或简单地更改:
lineConfig = gameObject.GetComponent<LineConfig> () ;
至:
lineConfig = gameObject.AddComponent<LineConfig>() ;
或者,如果您想确保已经附加了 LineConfig
,请将 [RequireComponent (typeof (LineConfig))]
添加到 class 的开头,高于 public class BrushWidth : MonoBehaviour
。
所以,你的 class 应该是这样的:
using UnityEngine ;
using System.Collections ;
using UnityEngine.UI ;
using System ;
using PaintCraft.Tools ;
public class BrushWidth : MonoBehaviour
{
public Button btn1, btn2, btn3, btn4, btn5, btn6 ;
public float brushSize ;
public Text brushNameTextBox ;
public String brushName ;
public LineConfig lineConfig ;
void Awake ()
{
lineConfig = gameObject.AddComponent<LineConfig> () ;
btn1.onClick.AddListener (() => SetThickness (1)) ;
btn2.onClick.AddListener (() => SetThickness (2)) ;
btn3.onClick.AddListener (() => SetThickness (3)) ;
btn4.onClick.AddListener (() => SetThickness (4)) ;
btn5.onClick.AddListener (() => SetThickness (5)) ;
btn6.onClick.AddListener (() => SetThickness (6)) ;
}
void Start ()
{
// Set Starting Thickness
SetThickness (3) ;
}
void SetThickness (int _lineNum)
{
switch (_lineNum)
{
case 1:
lineConfig.Scale = 0.1f ;
brushName = "Thin " ;
break ;
case 2:
lineConfig.Scale = 0.2f ;
brushName = "Light " ;
break ;
case 3:
lineConfig.Scale = 0.35f ;
brushName = "Regular " ;
break ;
case 4:
lineConfig.Scale = 0.5f ;
brushName = "Medium " ;
break ;
case 5:
lineConfig.Scale = 0.75f ;
brushName = "Thick " ;
break ;
case 6:
lineConfig.Scale = 1.0f ;
brushName = "Heavy " ;
break ;
default:
break ;
}
brushNameTextBox.text = brushName ;
}
}
希望对您有所帮助 :)