x


What is typing a variable? Is it assigning the value?

In this tutorial it talks about assigning a type to a variable. Is this assigning the value of the variable? If not what is it for?

more ▼

asked Sep 11 '10 at 09:47 PM

HTH_jjpprogrammer gravatar image

HTH_jjpprogrammer
42 5 5 6

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

3 answers: sort voted first

No, it's not assigning the value.

Basically, when you declare a variable and specify the type, you're telling the Unity compiler what the variable can hold.

In the tutorial, Will created a variable named thePrefab and said this variable can only store a GameObject in it.

You'll also notice he didn't assign a value, a GameObject, in the script. He actually assigned a GameObject using the Inspector when he dragged the BouncyBox prefab, which is a GameObject, into the thePrefab variable. In the Inspector, Unity displays the thePrefab variable as The Prefab.

more ▼

answered Sep 12 '10 at 01:30 AM

Terry gravatar image

Terry
96 1 1 7

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

Answering jjpprogrammer's answer (which is actually a question in itself).

I can't access the tutorial from here, but I assume it's doing this:

var thePrefab : GameObject;

function Start ()
{
   var instance = Instantiate(thePrefab, transform.position, transform.rotation);
}

The reason they're doing that is so you can access the instance of thePrefab that has been instantiated. If you want to change the position (or any property of that object), you can't access it by using 'thePrefab.position' (for example), because it's a link to a prefab, and not an actual GameObject.

For example:

function Update ()
{
   thePrefab.position.y += 1; // this would not work
   instance.position.y += 1;  // this would work
}

Check out the script reference for Object.Instantiate() for more information.

As I've commented above, you've actually asked a separate question inside an answer. Please keep questions as questions and answers as answers, as it helps to keep UnityAnswers easier to search/read for everyone.

more ▼

answered Sep 14 '10 at 12:25 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

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

Thanks! I have one more question. I think you might have answered this in your last answer but I am not sure . Why does he set the var instance to instantiate? I would think that assigning the instantiate to a variable would only set it to the variables value, not run it? Why not just say:

var thePrefab : GameObject;

function Start () {

Instantiate(thePrefab, transform.position, transform.rotation);

}

more ▼

answered Sep 13 '10 at 09:39 PM

HTH_jjpprogrammer gravatar image

HTH_jjpprogrammer
42 5 5 6

This should be posted as a separate question.

Sep 14 '10 at 12:20 AM Marowi
(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:

x822
x132
x123

asked: Sep 11 '10 at 09:47 PM

Seen: 750 times

Last Updated: Sep 11 '10 at 09:47 PM