Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by arstoykov · Aug 08, 2013 at 01:09 PM · c#instantiatecomponent

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);
         
     }
Comment
Add comment · Show 15
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ArkaneX · Aug 08, 2013 at 02:46 PM 0
Share

Could you check what is returned if you call GetComponent on the prefab itself?

 var result = block.GetComponent<BlockBehaviour>()

Is it null as well?

avatar image arstoykov · Aug 08, 2013 at 03:14 PM 0
Share

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

avatar image ArkaneX · Aug 08, 2013 at 05:53 PM 0
Share

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.

avatar image arstoykov · Aug 09, 2013 at 08:42 AM 0
Share

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...

avatar image ArkaneX · Aug 09, 2013 at 08:51 AM 0
Share

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?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image arstoykov · Aug 08, 2013 at 02:12 PM 0
Share

It's a prefab and it already has the script BlockBehaviour attached to it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges