- Home /
Question by
gdennis · Jan 18, 2015 at 12:04 PM ·
gameobjecttexture2daddcomponentguicontent
Object reference not set when GUIContent.image
I have 8 characters in my game. They all need to get the 'Twitter' script attached to it, so I can overlay a twitter on them. I am getting this error:
NullReferenceException: Object reference not set to an instance of an object Twitter.Awake () (at Assets/_Scripts/Social Media/Twitter/Twitter.cs:40) UnityEngine.GameObject:AddComponent()
The code where I try to add the Twitter script is as follows:
if (type == "twitter") {
GameObject npcWithTwitter = GameObject.FindGameObjectWithTag(poster.ToLower());
npcWithTwitter.AddComponent<Twitter>().sendTwitterDetails("test");
}
And my Twitter Awake method looks like this:
void Awake () {
twitterMainPageTexture = (Texture2D)UnityEngine.Resources.Load ("Images/Social Media/Twitter/mainpage", typeof(Texture2D));
twitterTweetTexture = (Texture2D)UnityEngine.Resources.Load ("Images/Social Media/Twitter/tweet", typeof(Texture2D));
content.image = twitterMainPageTexture; //the error is on this line
content2.image = twitterTweetTexture;
}
I can notice in my inspector that content.image is set to 'none' when I execute this script. I am stuck trying to figure out what I am doing wrong, especially since when I attach this script in the inspector manually to a gameobject everything just works fine (without any sort of code modifications)
Comment