Hi everyone, I started to use unity for some months ago. I usually have problem about using GetComponent…
In this case I’m using the FPS interface (so, scripts like : FPS input controller + Character Motor + Mouse Look) and I see that into the first and the second they are able to communicate using for example (into FPS Input Controller)
private var motor : CharacterMotor;
// Use this for initialization
function Awake () {
motor = GetComponent(CharacterMotor);
}
I try to do the same into MouseLook, but it is in C# and I have some problem… so I tried to enter in Mouse Look from Character Motor (with javascript), using similar code of before but it can’t recognize MouseLook :
Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js(242,26): BCE0018: The name ‘MouseLook’ does not denote a valid type (‘not found’).
Why?
Thanks in advance! =)
Javascript and C# scripts compile in different orders, you could always create a c# version of the mouse look script and then reference it. You can find a smooth mouse look script in c# for unity from here: http://wiki.unity3d.com/index.php/SmoothMouseLook
Okay I think I understand your question. I’ve had issues with this too, but I’ve found a way to fix it. I think you said you are using C#, so this is what I do:
I make a public transform variable just to hold the gameobject that the script you want to access is attached to.
public Transform target;
Then in the start function you set the script up like this:
void Start(){
scriptVariable = target.GetComponent<NameOfYourScriptHere>();
}
Then when you want to use something from that script, like a variable or something, you just use
scriptVariable.randomVariable = 1;
Depending on the name of your script and variables. I am pretty sure that answers your question, it was kind of hard for me to understand. If I am way off, just let me know and I’ll come remove my answer so you have a better chance at getting a right answer.