C# 更改线宽获取错误,对象引用未设置为对象实例
我希望能够改变线宽,但我似乎遇到了一点问题。我设置它的方式是,我有6种不同的线宽,当用户单击某个宽度时,它应该在下次绘制线条时反映出来。目前,我得到了一个错误: “NullReferenceException:对象引用未设置为对象BrushWidth.SetThickness(Int32 _lineNum)(位于Assets/_Scripts/BrushWidth.cs:46)的实例BrushWidth.Start()(位于Assets/_Scripts/BrushWidth.cs:30)” 这是指改变线比例的线,lineConfig.scale=0.35f 我在这里遗漏了什么?我以为我用public LineConfig LineConfig引用了我的实例;谢谢你的帮助C# 更改线宽获取错误,对象引用未设置为对象实例,c#,unity3d,instance,C#,Unity3d,Instance,我希望能够改变线宽,但我似乎遇到了一点问题。我设置它的方式是,我有6种不同的线宽,当用户单击某个宽度时,它应该在下次绘制线条时反映出来。目前,我得到了一个错误: “NullReferenceException:对象引用未设置为对象BrushWidth.SetThickness(Int32 _lineNum)(位于Assets/_Scripts/BrushWidth.cs:46)的实例BrushWidth.Start()(位于Assets/_Scripts/BrushWidth.cs:30)” 这
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 ;
}
}
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
使用制度;
使用油漆工艺、工具;
公共类:单一行为
{
公共按钮btn1、btn2、btn3、btn4、btn5、btn6;
公众浮子大小;
公共文本框;
公共字符串名称;
公共LineConfig LineConfig;
无效唤醒()
{
lineConfig=gameObject.GetComponent();
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));
}
无效开始()
{
//设置起始厚度
设置厚度(3);
}
空隙集厚度(int _lineNum)
{
开关(_lineNum)
{
案例1:
lineConfig.Scale=0.1f;
brushName=“Thin”;
打破
案例2:
lineConfig.Scale=0.2f;
brushName=“Light”;
打破
案例3:
lineConfig.Scale=0.35f;
brushName=“常规”;
打破
案例4:
lineConfig.Scale=0.5f;
brushName=“中等”;
打破
案例5:
lineConfig.Scale=0.75f;
brushName=“厚”;
打破
案例6:
lineConfig.Scale=1.0f;
brushName=“重型”;
打破
违约:
打破
}
brushNameTextBox.text=brushName;
}
}
您的问题在于脚本所附加到的对象没有附加LineConfig
组件。因此,GetComponent
返回null。若要解决此问题,请向对象添加LineComponent
,或者只需更改:
lineConfig = gameObject.GetComponent<LineConfig> () ;
希望这有帮助:)NO!
Awake()
在调用Start()
@user3185569 Awake之前执行,然后调用Start。@OP将附加有LineConfig
的游戏对象拖动到LineConfig
插槽。在脚本之前添加[RequireComponent(typeof(LineConfig))]
属性
lineConfig = gameObject.AddComponent<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.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 ;
}
}