Using addcomponent won't run the script

Hello, I am using addcomponent in another script and it is supposed to put the following on another gameobject:

#pragma strict
var gamecam : GameObject;


function Start(){
gamecam = GameObject.Find("guicamera");
}

function Update(){
Start();
}

function OnTriggerEnter(){
gamecam.active = false;
}

UPDATE: I have tried using coroutines and calling them from the update function. Unfortunately that camera is still not being assigned. If you have an idea of something that might mimic this process, let me know. My objective is just to have this script dormant until I need it. Disable/Enable seems to only work with Update functions, so that is not helping since this uses OnTriggerEnter

The problem is that the gui camera is not being assigned. In the inspector, it shows that the script is being added, but nothing is happening after that. The script is not assigning the gamecam variable. I read through: AddComponent passing variable before Start/Awake - Unity Answers and that’s where I got the idea to call the start function from update. However it is not working. What is going on?

If you are using AddComponent to add this script, I don’t believe Start will run.

  • I am not sure about this.
    You could try using a short loop, but this is probably not a good way to do it:
    Before Update:

    var :int;

In Update:

if(<name><1){
    name++;
    //Add the component on this line
    //(For the script which adds the component)

}

Also, you may want to check if the script is enabled:

if(<script>.enabled==true){
        Debug.Log("Script enabled");
}

Hey, im New on this but i actually saw this problem, May you can put the gamecam as a public variable.
And im not sure if its a good idea to call the function Stat() in the update… Why u dont try to create other function for that?
I Hope my answer could help you.

Start is called whenever we attach new script to object at runtime. So either GameObjecct.Find can not find the object. It can happen if object you are trying to find is disable in Hierarchy.

Make sure that the camera is named properly as “guicamera”.
Check in the Hierarchy windows if there is one.

Sometimes if you instantiate the camera from prefab it will be named “guicamera (Cloned)”, that will not work.
Make sure it’s exactly “guicamera”