Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Kebabs · Dec 06, 2013 at 05:35 PM · nullreferenceexceptioninstancegameobject.findobject reference

Gameobject.find & my error NullReferenceException: Object reference not set to an instance of an object

Hi I am trying to instantiate a prefab and add the prefab to a gameobject that already exists in the world. I have researched the error and it seems that I need to declare the gameobject I want to use as the parent in the start method but I am confused. Here is my code...

 void OnTriggerEnter(Collider collider)
 {
     GameObject stageSegment = Instantiate(A, new Vector3 (0,0,0), Quaternion.identity) as GameObject; 
     stageSegment.transform.parent = GameObject.Find("Stage Scroll").transform;
     Debug.Log(GameObject.Find("Stage Scroll"));
     
 }

And this is the error I get from the Debug.log output...

NullReferenceException: Object reference not set to an instance of an object LevelLoadingTrigger.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/LevelLoadingTrigger.cs:78)

The gameobject I want to use as the parent already has a transform component so do I still need to declare it in Start and if so how do I go about declaring the gameobject I want to make the parent. The object instantiates just fine but it makes a clone of it in the hierarchy instead of becoming a child of my Stage Scroll game object - which only has a script attached to it and a transform component.

I am still new to learning C# and any help is much appreciated.

Comment
Add comment · Show 17
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 karlhulme · Dec 06, 2013 at 08:12 PM 1
Share

Your code looks correct to me. The NullReferenceException from your debug line is telling you that Unity is not able to find an object named "Stage Scroll". Perhaps you spelt it without a space, using different casing, or maybe its not active?

avatar image Kebabs · Dec 06, 2013 at 09:16 PM 0
Share

I tried that already dude - with/without spaces, camel casing. I've tried all sorts. When you say if its active or not, what do you mean?

avatar image Kebabs · Dec 06, 2013 at 09:20 PM 1
Share

The stage scroll object in question is an object in my hierarchy. Like I have said above its just got a transform component on it a script which moves the object across the screen at a s$$anonymous$$dy pace. Its an empty gameobject in the sense that it has no 3D objects attached to it, just a transform component to allow movement. alt text

untitled-1.jpg (128.4 kB)
avatar image karlhulme · Dec 06, 2013 at 09:27 PM 0
Share

thumbs up for the excellent diagram!

active objects have a tick to the left of their name in the inspector. inactive objects don't have a tick (obviously!) and also appear dimmed in the hierarchy. looking at your diagram I don't think that's the problem. hmm, i'm stumped.

avatar image chillersanim · Dec 06, 2013 at 09:58 PM 1
Share

Sry didn't see new posts, whas to fast...
You are declaring A as a transform in the header.
But you are casting it to a GameObject, that results in a null.

Ins$$anonymous$$d you should change the declaration to "public GameObject A"

Edit:
Argh, again to late... :D

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kebabs · Dec 06, 2013 at 10:10 PM

OK guys it is now working :)

THANK YOU BOTH @chillersanium and @karlhulme

I just had to ctrl+z back to my default code from the start of the question, make A a public gameobject and now when the game is running it becomes a child of stage scroll!!!!!

Thank you both for the help!! I'm so new to coding and help like this inspires me to keep learning and understanding why code works/does not work. THANK YOU SOO MUCH!! This is for my studies so seriously again THANK YOU BOTH :)

 void OnTriggerEnter(Collider collider)
 {
     GameObject stageSegment = Instantiate(A, new Vector3 (0,0,0), Quaternion.identity) as GameObject; 
     stageSegment.transform.parent = GameObject.Find("Stage Scroll").transform;
     Debug.Log(GameObject.Find("Stage Scroll"));
     
 }

alt text


untitled-1.jpg (141.0 kB)
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 chillersanim · Dec 06, 2013 at 10:15 PM 0
Share

Looks like somebody got happy :D

Was happy to help, for that is the Answers section.
Tipps for further questions:

  1. Do give the full code

  2. Also point out on which line the error is pointing (if you don't give the whole header to)

  3. Perhaps try do debug first, in $$anonymous$$onoDevelop that is a real easy task

Greetings Chillersanim

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

18 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 avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 1 Answer

NullReferenceException help 1 Answer

Pause Script Suddenly Stops Working 0 Answers

Activating a game object that is inactive through inspector 3 Answers

NullReferenceException with an Array 2 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