We need to reference a component from another object, that isn’t parented, through a script in JavaScript.
We are very new to coding in Unity and are not quite getting it…
How exactly do we use GetComponent on a separate object?
(Please explain how to use Accessing Other Objects
and
GameObject.GetComponent)
We are trying to access the ‘Sphere’ objects Mesh Renderer and enable/disable it using a script that isn’t a component on it. While also accessing the ‘EthanBody’ Skinned Mesh Renderer component and being able to do the same thing.
Also it would be nice to know how to access an integer value from Morph script so if it becomes active it will run Morph1.
The code is just us goofing around a little trying new things, it doesn’t actually work right now (Obviously)
Morph:
#pragma strict
private var onoff: boolean = true;
var Body;
public int Invis;
//FUNCTION START
function Start () {
Body = GameObject.Find("ThirdPersonController/EthanBody");
print(Body);
}
//FUNCTION UPDATE
function Update () {
//GET KEY "G"
if(Input.GetKeyDown('g')){
onoff = !onoff;
if (onoff){
GetComponent(MeshRenderer).enabled = false;
Invis = true;
} else {
GetComponent(MeshRenderer).enabled = true;
Invis = false;
}
};
}
//Morph1 I.E. Different Script
#pragma strict
public var Morph : GameObject;
private var onoff: boolean = true;
//FUNCTION START
function Start () {
}
//FUNCTION UPDATE
function Update () {
//GET KEY "G"
if(Morph.gameObject.GetComponent(Morph).Invis == true){
onoff = !onoff;
if (onoff){
GetComponent(SkinnedMeshRenderer).enabled = true;
} else {
GetComponent(SkinnedMeshRenderer).enabled = false;
}
};
}