- Home /
Question by
tasti man LH · Sep 07, 2015 at 03:47 AM ·
getcomponentplatformer
Using GetComponent to get a script
So I'm trying to access a script using GetComponent, but I'm not sure how.
This was originally apart of a Unity 3D scene but now I'm trying to convert the code over to 2D. At first I was using the default Platform Input Controller to control my character, but I ended up making a new controller script from scratch. So that means I should be trying to reference the script. But apparently the "Script" reference does not exist. Because of that, I am at a loss on how to reference the script.
(here are the relevant parts of said script)
#pragma strict
var ChController : Transform;
var elevator : Transform;
var climbNow : boolean = false;
var isClimbing : boolean = false;
var heightFactor : float = 3.0;
var speed : float;
private var PlatInput : PlatformInputController;
function Start () {
PlatInput = GetComponent("CharacterControlTest");
}
function OnTriggerEnter(Col : Collider){
if(Col.gameObject.tag == "Climber" /*&& Input.GetKey("e")*/){
PlatInput.enabled = false;
if (Col.attachedRigidbody) {
Col.attachedRigidbody.isKinematic = false;
}
climbNow = !climbNow;
}
}
function OnTriggerStay(Col : Collider){
if(Col.gameObject.tag == "Climber"){
if (Col.attachedRigidbody) {
Col.attachedRigidbody.isKinematic = true;
}
print("stop");
isClimbing = false;
}
}
function OnTriggerExit(Col : Collider){
if(Col.gameObject.tag == "Climber"){
PlatInput.enabled = true;
Col.attachedRigidbody.isKinematic = false;
climbNow = false;
}
}
Comment
Answer by Cherno · Sep 07, 2015 at 06:36 AM
The component returned by GetComponent needs to be of the same type as the variable it is assigned to.
private var PlatInput : CharacterControlTest;
function Start () {
PlatInput = GetComponent("CharacterControlTest");
}
or
private var PlatInput : CharacterControlTest;
function Start () {
PlatInput = GetComponent("CharacterControlTest");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612015450im_/https://answers.unity.com/themes/thub/images/avi.jpg)