Problems C# and Tutorial: Introduction to Scripting

I am new to Unity and try to master the scripting part. I have knowledge in C# and want use it in my project. I had the idea to use Unity for 3D scenes but because these scenes are only a part of my project Javascript does not help much. The whole project will be written in Visual C# (or VB 2008).

After going to the GUI tutorial i am struggling in the scripting tutorial. The second task is to write a little script (name “Follow”) and attach it to a component.

Here is the original code (Javascript):

var target : Transform;
function Update () {
transform.LookAt(target);
}

My C# translation (using Visual C# Express) looks so:

using UnityEngine;
using System.Collections;

public class Follow : MonoBehaviour 
{
    Transform target;
    
	// Use this for initialization
	void Start () 
    {
	
	}
	
	// Update is called once per frame
	void Update () 
    {
        transform.LookAt(target);
	}

The next step is

Attaching the script works but no “target” variable is here somewhere exposed - no warning in the IDE. And so the following step does not work:

As i have no “target” i can not drag the Main Camera on the spotlight and whole script (and the next also) does not work.

What did i wrong ? Thanks.

Try making the target public in your c#, ie

public class Follow : MonoBehaviour
{
    public Transform target; 
...

That should expose the target in the inspector for you.

Thanks. Missing “Public” was the reason.

In any case, you should know that not writing “public” or “private” before a variable declaration equals to declaring it as a private variable, meaning that this variable can only be accessed from the script where you declared it (and, moreover, you cannot use the unity gui to drag a gameobject on it…).

A suggestion I would give to you is to never assign things via the unity gui, but only via code: for much larger projects it will really help you out.

Moreover, if you needed to declare this as a private variable, you could assign it only via code.

example code would be:

using UnityEngine;
using System.Collections;

public class Follow : MonoBehaviour
{
   private Transform target;
   
   // Use this for initialization
   void Start ()
   {
       target = Camera.main.transform
   }
   
   // Update is called once per frame
   void Update ()
   {
        transform.LookAt(target);
   }

Could you explain the reasoning behind this? I’m also starting off with Unity using C# and I’ve been advised to do the precise opposite.

@Philip

I don’t agree with that. Hardcoding values is what causes large projects to become spaghetti code.

Any object can look at another object. This particular script could be applied to any object. you could even have one cube look at another cube if you want to. It doesn’t always have to be a camera.

That totally blows the whole idea of Object Oriented design out of the water. One of the main aspects of OOP is code reuse.

I would only have to say. If you use Unity you have two options. You can set a public variable in the Gui or in code but only do it one of the two ways for consistancy.

based on that unite presentation from a few years ago, and in my own extremely small novice experience, i would say assigning values through the editor is fine and can save you a ton of work, but assigning target gameobjects in the gui is usually a bad idea for projects of more than a very small size (if your game will have multiple stages, say) because you’ll end up doing more work and introducing more errors than if you set up a system to grab the right objects/component references at runtime, say, with tags.