A couple of coding questions

I’m an experienced C++ programmer and am getting familiar with coding inside of Unity. Here are a couple of questions…

  • What is the difference between print(“…”) and Debug.Log(“…”). They both seem to do the same thing except that they are prefaced slightly differently in the console.

Javascript scope question
I’m doing a simple test translation as follows:

var speed = 0;

function Update()
{
  if (transform.position.y >= 3)
    speed = -10;
  else if (transform.position.y <= 0)
    speed = 10;

  transform.Translate(0, Time.deltaTime * speed, 0, Space.World);
}

If I don’t declare the “speed” variable above the function, speed gets set to zero on the 2nd iteration. Why?

If I declare the “speed” variable locally inside the Update() function, the same thing happens. Why?

Thanks,

Brian

if you declare it in the function, its local to the function and is gone once you leave the function.

There isn’t much. print() only works from Monobehaviours (and not, for example, your helper classes), so using Debug.Log is a good habit.

For your second question, it’s because it become a local variable which is redeclared in every frame (that is, in every Update() )

I know that. But it doesn’t answer my question. The variable should be local. My question is why when I declare it as local (or don’t declare it at all), does it behave strangely? The problem is that it isn’t taking on the correct value.

– Brian

For your second question, it’s because it become a local variable which is redeclared in every frame (that is, in every Update() )[/quote]

Yes, but when it is declared as local, or left undeclared. I’m getting an unexpected value after the first assignment. The assignment doesn’t seem to work at all. As far as I can tell this contrary to the purpose of a local variable. Am I missing something about the way Update is called?

– Brian

For your second question, it’s because it become a local variable which is redeclared in every frame (that is, in every Update() )[/quote]

Yes, but when it is declared as local, or left undeclared I’m getting an unexpected value after the first assignment. The assignment doesn’t seem to work at all. As far as I can tell this contrary to the purpose of a local variable. Am I missing something about the way Update is called?

– Brian

It shouldn’t have some strange value after you assigned something. Before that point its naturally possible as you have an unassigned variable
try the following

function Update()
{
  var speed : float = 0.0;
  if (transform.position.y >= 3)
    speed = -10;
  else if (transform.position.y <= 0)
    speed = 10;

  transform.Translate(0, Time.deltaTime * speed, 0, Space.World);
}

I just tried exactly what you did and it doesn’t work. The only way it works is to declare speed outside of the function body. Maybe you can give it a try to see what I mean.

– Brian

I just figured it out. It was my coding error. Speed doesn’t evaluate to anything unless transform.position.y >=3 or <=0. It was so simple, I blamed Unity instead of myself!

– Brian