Confused by the procedure for getting script references(solved)

Getting things like transform gameobject etc use a generic term GameObject etc but scripts are different there seems to be no generic term?.
The below code shows me getting a script reference to magicuser a script on mage a GameObject thats a child of Wizard the Parent GameObject.If i use anyother name but the name of the script it returns a does not exist. Why can gameObject etc have a madeup name but a script need to have the exact name of the referenced script?

using UnityEngine;
using System.Collections;

public class movepart : MonoBehaviour {
 
    Vector3 head;
     Transform transforms;
    //magicuser playerScript;
    public void Start()
        {
        GameObject Mage = GameObject.Find ("mage");
        magicuser playerScript = Mage.GetComponent<magicuser> ();//have to use magicuser
        head = playerScript.targets;

        }

    public void Update () {
        transform.position = Vector3.Lerp(transform.position, head , Time.deltaTime);
        }
    }

Why not?

[code=CSharp]using UnityEngine;
using System.Collections;

public class movepart : MonoBehaviour {

    Vector3 head;
     Transform transforms;
  
    public void Start()
        {
        GameObject Mage = GameObject.Find ("mage");
        Script playerScript = Mage.GetComponent<magicuser> ();//this would keep it inline with the other getcomponent calls
        head = playerScript.targets;

        }

    public void Update () {
        transform.position = Vector3.Lerp(transform.position, head , Time.deltaTime);
        }
    }

[/code]

GameObject isn’t a “generic name”. It’s the name of the thing it is. It’s also other things - like a UnityEngine.Object. Just Like a Transform is also a Component and every script you write is also a MonoBehaviour (which is also a Component).

It’s like you walk up to a dog and say “I don’t want to call you a dog. Why can’t I call you an animal? Or a pet?” Well, you can. No one is stopping you. It just might not be specific enough for what you’re trying to do.

“Hey - go walk the animal.” Which one? The cat? The hamster?

In terms of replacing “magicuser” with “Script” - if you replace it with “MonoBehaviour” it would work but your next line would fail because MonoBehaviour isn’t specific enough.

you could use polymorphism

// NOTE this code isn't tested and may not work
MonoBehaviour script = GetComponent<magicuser>();

EDIT: Sorry didn’t see previous post

Alternately, to avoid the lack of specificity you can just use var

var script = GetComponent<magicuser>();

Ok now i’m even more confused var is a javascript term how does it now work in my c# script???
The var does work. when i replace it with a int the c# version of var it fails huh?

tawdry,
var in C# :

added:

It really sounds like you need to do some reading on strongly typed languages

hmm messed up that they use a js command and change its meaning in c# so I can use var for everything GO Transform etc ill just use it for scripts and pretend i’m misspelling Script.Probably slightly more overhead using var as the computer has to figure out what its meaning must be

I wouldn’t recommend doing that :slight_smile:

I’m not trying to tell you how to write your code but when you get into this kind of business it’s bad news bears

var theThing = you.GoGetTheThing();

You probably should read the documentation.
Once the code is compiled the IL code is indistinguishable.