- Home /
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; } }
I have a feeling you shouldnt be parenting rigid bodies unless you are sure about the object this script runs on isn't moving.
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.
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).
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 :)
;-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!