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
0
Question by Jason Hamilton · Jan 07, 2011 at 09:09 AM · transformnullreferenceexceptionstringfindlink

NullReferenceException when transform.Find(string) STILL

hey, heres a video of my problem, sorry if I'm doing anything convoluted, if you can help that would be great thanks. I don't know whats the deal withvideo links on this site so sorry if it's against the rules; http://vimeo.com/18361778 if you notice anything else wrong or unnecessary feel free to mention it :P here is the script which is applied to the gemspawner object :

var Gem1 : Transform; var Gem2 : Transform; var Gem3 : Transform; var Gem4 : Transform; var Gem5 : Transform; var Gem6 : Transform; var Gem7 : Transform; var isShooter = false; var whichGemIGot : String;

function Start () { CreateGem(); }

function Update () { if(Input.GetButtonDown("Jump") && isShooter) { var child = transform.Find(whichGemIGot); child.rigidbody.AddRelativeForce(0,-1000,0); isShooter = false; } }

function CreateGem () { var whichGem = Random.Range(1,7); switch(whichGem) { case 1: var Gem11 = Instantiate(Gem1, transform.position, transform.rotation); Gem11.parent = transform; whichGemIGot = "Gem - Blue(Clone)"; break; case 2: var Gem22 = Instantiate(Gem2, transform.position, transform.rotation); Gem22.parent = transform; whichGemIGot = "Gem - Green(Clone)"; break; case 3: var Gem33 = Instantiate(Gem3, transform.position, transform.rotation); Gem33.parent = transform; whichGemIGot = "Gem - Indigo(Clone)"; break; case 4: var Gem44 = Instantiate(Gem4, transform.position, transform.rotation); Gem44.parent = transform; whichGemIGot = "Gem - Orange(Clone)"; break; case 5: var Gem55 = Instantiate(Gem5, transform.position, transform.rotation); Gem55.parent = transform; whichGemIGot = "Gem - Red(Clone)"; break; case 6: var Gem66 = Instantiate(Gem6, transform.position, transform.rotation); Gem66.parent = transform; whichGemIGot = "Gem - Violet(Clone)"; break; case 7: var Gem77 = Instantiate(Gem7, transform.position, transform.rotation); Gem77.parent = transform; whichGemIGot = "Gem - Yellow(Clone)"; break; } }

Comment
Add comment · Show 1
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 Statement · Jan 07, 2011 at 05:04 PM 0
Share

I have a feeling you shouldnt be parenting rigid bodies unless you are sure about the object this script runs on isn't moving.

1 Reply

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

Answer by Jessy · Jan 07, 2011 at 04:45 PM

It's hard or impossible to tell, from what you've provided (I did watch the video), what the problem is. I think it either has something to do with other code you're running, or you misspelled something. The reason the latter is a possibility, is that you're basing your code on strings. Not only does that make for less efficient code, but it also allows for errors to occur, that won't be picked up as errors, until you actually use the strings in-game.

I don't understand what "isShooter" is, so I'm leaving that out. I'll leave it up to you to verify that it isn't causing a problem.

var Gems : Rigidbody[]; // Resize and populate this array in the Inspector. var force : Vector3; private var whichGemIGot : Rigidbody;

function Start () { CreateGem(); }

function Update () { if (Input.GetButtonDown("Jump")) { whichGemIGot.AddRelativeForce(force, ForceMode.Impulse); } }

function CreateGem () { whichGemIGot = Instantiate(Gems[Random.Range(0, Gems.Length)], transform.position, transform.rotation) as Rigidbody; whichGemIGot.transform.parent = transform; }

Any time you find yourself imagining a numbered list of variables, it's a good bet that an array is a better way of storing and using the data. For Gems, It doesn't matter what you store them as, because Unity will reference the whole prefab. Everything has a Transform, so using Rigidbody[] is just a way to ensure that they all have that component. (If I were to use code like this in a project of my own, I would make "Gem" into a class that stored both a Rigidbody and a Transform, but this isn't necessary. It would just make the code use fewer CPU cycles.)

I changed your ForceMode to Impulse, because the default, ForceMode.Force, is designed to be applied over time, not instantaneously. (Multiplying what you'd use, if you kept it as ForceMode.Force, by Time.fixedDeltaTime, yields the number that makes sense.)

I would try to apply this different, array-based and string-less method, instead of trying to hunt down errors in your more verbose and complex code. If you still have null reference errors after doing so, it's probably best to edit this question until we can get this fixed for you.

Comment
Add comment · Show 3 · 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 Statement · Jan 07, 2011 at 05:12 PM 0
Share

There's a bug in this code. Random.Range should probably not range from 1 to 7. You probably want to use Random.Range(0, Gems.Length).

avatar image Jason Hamilton · Jan 08, 2011 at 03:01 PM 0
Share

WOW!, i want your babies, after siting down and having a long hard think about ALL of this i get it, all except the 'as rigidbody' bit, what does that do?. but thanks for solving this your a legend, i present you with the award for helping the biggest noob :)

avatar image Jessy · Jan 08, 2011 at 04:08 PM 0
Share

;-D I used "as Rigidbody" because Instantiate returns an object, not a Rigidbody. If you're not using Unity iOS, Unity can do this for you, but it's not as efficient code. You can decide for yourself if you care, but using #pragma strict at the top of your scripts will generally alert you to when your JavaScript isn't being as efficient as it could be (C# always forces you to code like that). Good job making your video, by the way. Sure, you have a lot to learn (we all do ;-)), but you gave it a great try, and have a good attitude. $$anonymous$$eep it up!

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

No one has followed this question yet.

Related Questions

transform.Find(string)? 2 Answers

Why am I getting a nullReferenceException? 1 Answer

Find a Position on the Axis of a GameObject 1 Answer

Transform.Find failing to find named child in clone 1 Answer

Why does this Instantiate give a Null Reference Exception? 1 Answer


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