- Home /
transform.Find(string)?
hey, can i use a string variable (whichGemIGot) that is equal to a gameObjects name (Gem - Blue) ??? what i have now gets NullReferenceException
I know this is probably wrong i just cut out the bits that seemed needed to understand my situation. :P
var whichGemIGot : String; function Update () { if(Input.GetButtonDown("Jump") && isShooter) { var child = transform.Find(whichGemIGot); child.rigidbody.AddRelativeForce(0,-1000,0); transform.DetachChildren(); 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";
Answer by Mike 3 · Dec 28, 2010 at 01:21 PM
Remember that instantiated objects have (Clone) on the end of their name by default
It'll my bet is you need to search for "Gem - Blue (Clone)" instead
Thankyou, i did add this, but it still doesn't work :'(
scratch that, i added a space but didn't need to :P
im still having problems, i made a video of the problem; http://vimeo.com/18361778
Answer by StephanK · Dec 28, 2010 at 12:06 PM
Yes you can. If no child with that name exists the function will return null.
but i can see the object at runtime, it still doesn't work?
Is it a child of the object that your script is attached to? Is it a direct child?
the script is applied to the parent, the child is a direct child
im still having problems, i made a video of the problem; http://vimeo.com/18361778