- Home /
Null Reference Exception on instantiated object's script.
Hello, I'm trying to get a script component from an instantiated object, but when I try to access any members from the script it gives me Null Reference Exception. The BlockBehaviour script is attached to the game object.
public GameObject block;
public GameObject gBlock;
gBlock = (GameObject)Instantiate(block, this.transform.position, Quaternion.identity);
if (gBlock){
//Tell the DragCamera script that we are holding a block
DragCamera.onBlock = true;
//Get the block script
//bb = (BlockBehaviour)gBlock.GetComponent<BlockBehaviour>();
//Set alpha level to 0.6
gBlock.GetComponent<BlockBehaviour>().setTransparent(0.6f);
}
Could you check what is returned if you call GetComponent on the prefab itself?
var result = block.GetComponent<BlockBehaviour>()
Is it null as well?
It's not null when I call it on the prefab, but it isn't null when I call it on the instantiated gameobject as well. When I call a member from BlockBehaviour (no matter if it is on the prefab or on the instantiated block itself), then I get the error: NullReferenceException: Object reference not set to an instance of an object
Then I believe the error must be related to the BlockBehaviour class itself. When you call setTransparent, you're probably setting transparency of some object - are you sure it's not null?
I might be able to help more, if you could post the error details from Unity console.
Hi!
The only error I get is this (When I call setTransparent):
NullReferenceException: Object reference not set to an instance of an object BlockBehaviour.setTransparent (Single index) (at Assets/Scripts/BlockBehaviour.cs:196) InstantiateBlocks.On$$anonymous$$ouseDown () (at Assets/Scripts/InstantiateBlocks.cs:40) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)
Here is the setTransparent method from BlockBehaviour:
private tk2dBaseSprite sprite = null;
void Start() {
sprite = GetComponent<tk2dBaseSprite>(); // Get the tk2dBaseSprite component and store it in
}
public void setTransparent(float index){
Color textureColor = sprite.color;
textureColor.a = index;
sprite.color = textureColor;
}
I checked wheter sprite is null when I run setTransparent, and it is null for some reason :(
I also tried to call other members from BlockBehaviour, but the same error appears...
Are you sure 'tk2dBaseSprite' component is added to the BlockBehaviour? What is the value of 'sprite' after execution of the single line inside Start method?
Answer by ArkaneX · Aug 09, 2013 at 01:48 PM
After quite long conversation, it looks that the part of the problem was initiating the variable inside Start() method. Changing this to Awake() was a step towards fixing the error.
Answer by Mill0698 · Aug 08, 2013 at 01:48 PM
Make sure that, that GameObject actually already has the script BlockBehaviour. If it is a prefab, then add the script before runtime. If it is just a random GameObject. Try adding the Script before trying to access it.
It's a prefab and it already has the script BlockBehaviour attached to it.
Your answer
Follow this Question
Related Questions
C# Adding Components From Other Gameobjects 3 Answers
AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers
How do i Instantiate a prefab with specific assests included 3 Answers
C# Instantiating a Gameobject with a Flat Sphere Collider 1 Answer
C# How to Check If a Gameobject Contains a Certain Component 6 Answers