Hello i got a Simple problem.
I’m avoiding to use the .GetComponent in my Update function. I got some really bad performance because i used to much of these. I will make a example.
var thePlayer : GameObject
var theDoor : GameObject
function Update()
{
var ray : Ray = thePlayer.camera.ViewportPointToRay (Vector3(0.5,0.5,0));
var lenght : float = 5.0;
var hit : RaycastHit;
if(Physics.Raycast(ray, hit, lenght))
{
hitObject = hit.collider.gameObject;
if(hitObject.gameObject.tag == "Door")
{
if(Input.GetKeyDown(KeyCode.E))
{
Attacked();
}
}
}
}
function Attacked ()
{
player.camera.enabled = false;
cameraSnow.SetActive (true);
yield WaitForSeconds (0.01);
player.camera.enabled = true;
cameraSnow.SetActive (false);
theDoor.GetComponent(RandomScript).enabled = true;
}
So that’s just one random example. My actuall script is much larger that’s why i need to define the .GetComponent types in the Start or Awake function and call enable/disable them in a Update function. However I’m pulling my hair out of this. I didn’t figured out how you actually do this in Javascript. It has plenty of answers in C# but yeah… I searched about 1 hour in Google for the answer and checked the Scripting Reference… I even posted my first question on Unity. So either I am completely
incompetent or just stupid. Im sorry for bothering you guys and i also apologize for my bad english…
Yes i would like to but in my real script i use it that much, It can’t call everything on every frame and some commands don’t even get executed and im just standing there, without an error nothing.
Make a public variable with the same name as your script from which you want the variable from and in the inspector just drag and drop the second script. In your example you have a RandomScript variable, in your other script have this code
public var randomScript : RandomScript
and in the inspector you should be able to drag and drop the script or you can do the following
function start()
{
randomScript = GameObject.Find(“Door”).GetComponenet(“RandomScript”);
}
that will get the script and set the script variable to that and you should be able to access its variables
Yes that’s exactly what I wanted but the final question is if i use you second example with the start function, how do i actually enable and disable that component?
Hello i got a Simple problem.
I’m avoiding to use the .GetComponent in my function. I got some really bad performance because i used to much of these. I will make a example.
function FireOneShot () {
//No shooting when sprinting
if (canShoot){
var target = transform.GetComponenet(“AI”).Target;
(Assets/!Realistic FPS Prefab Files/~Demo Scene Assets/~AlienNPC/Scripts/Gun.js(102,40): BCE0019: ‘GetComponenet’ is not a member of ‘UnityEngine.Transform’. )