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);
}
}
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.
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?
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