- Home /
Swapping out a render texture on a Raw Image via c# script.
Can someone PLEASE show me a basic example of how this is done. I have a raw image attached to a panel of the new GUI system. I have a list of gameobjects that Is created at runtime and I can target by hitting the tab key.
I want to be able to switch Render Textures that is on the GUI image to the camera or texture assigned to that camera as I tab through them but am unable to get it to work. I end up with a runtime error of "NullReferenceException: Object reference not set to an instance of an object" my debug shows me the correct render texture is being selected as I hit tab. My raw image is being selected via the inspector. I have tried so many different ways to make this work I cant even count. So no clue what i am doing wrong. I am able to swap a basic image, the objects name and other references on the Panel. I have only been coding for 3 months so still a bit of a noob...
<pre>
public RawImage rawImg;
public void GetInfoFromSelectedTarget()
{
if (TargetController.instance.selectedTarget.gameObject == null)
{
TargetSelected = false;
FillImage.fillAmount = 0f;
Name.text = null;
Race.text = null;
Level.text = null;
rawImg = null;
}
else
{
Transform tmp = TargetController.instance.selectedTarget;
FillImage.fillAmount = tmp.GetComponentInChildren<EnemyHealth> ().e_percHealth;
Sprite pic1 = tmp.GetComponentInChildren<EnemyInfo> ().MyPic;
Name.text = tmp.GetComponentInChildren<EnemyInfo> ().Name;
Race.text = tmp.GetComponentInChildren<EnemyInfo> ().race;
Level.text = "Level: " + tmp.GetComponentInChildren<EnemyInfo> ().level.ToString ();
txtForRndTxt = tmp.GetComponentInChildren<EnemyInfo> ().RendTxt;
rawImg.texture = txtForRndTxt;
// Debug.Log ("i am trying to show this texture: " + tmp.GetComponentInChildren<EnemyInfo> ().RendTxt.ToString());
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613192658im_/https://answers.unity.com/themes/thub/images/avi.jpg)