x


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";
more ▼

asked Dec 28 '10 at 12:01 PM

Jason Hamilton gravatar image

Jason Hamilton
445 68 73 80

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Dec 28 '10 at 01:21 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 252

Thankyou, i did add this, but it still doesn't work :'(

Dec 28 '10 at 01:56 PM Jason Hamilton

scratch that, i added a space but didn't need to :P

Dec 28 '10 at 02:08 PM Jason Hamilton

im still having problems, i made a video of the problem; http://vimeo.com/18361778

Jan 04 '11 at 06:06 AM Jason Hamilton
(comments are locked)
10|3000 characters needed characters left

Yes you can. If no child with that name exists the function will return null.

more ▼

answered Dec 28 '10 at 12:06 PM

StephanK gravatar image

StephanK
6k 39 53 93

but i can see the object at runtime, it still doesn't work?

Dec 28 '10 at 12:42 PM Jason Hamilton

Is it a child of the object that your script is attached to? Is it a direct child?

Dec 28 '10 at 01:16 PM StephanK

the script is applied to the parent, the child is a direct child

Dec 28 '10 at 01:57 PM Jason Hamilton

im still having problems, i made a video of the problem; http://vimeo.com/18361778

Jan 04 '11 at 06:06 AM Jason Hamilton
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5051
x1273
x407
x394
x204

asked: Dec 28 '10 at 12:01 PM

Seen: 1932 times

Last Updated: Dec 28 '10 at 12:01 PM