• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JA_555 · Oct 20, 2014 at 05:19 PM · addcomponent

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: http://answers.unity3d.com/questions/526058/addcomponent-passing-variable-before-startawake.html and that's where I got the idea to call the start function from update. However it is not working. What is going on?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bob1234 · Oct 21, 2014 at 05:22 AM 0
Share

hey could you link the other script please? and i dont think you can call start in update, since start initializes when the scene loads, ie all the stuff you want loaded at "start" goes to the start function, im not 100% sure about this tho, just a thought

avatar image bubzy · Oct 21, 2014 at 06:35 AM 0
Share

you can add a line in start to say

 if (gamecam!= null)
 {
 Debug.Log("Got the Camera");
 }

while this wont fix your problem, you will at least know if the gamecam has been assigned correctly

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by roboaedin · Oct 20, 2014 at 06:12 PM

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 <name>: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");
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JA_555 · Oct 21, 2014 at 04:35 AM 0
Share

I honestly don't understand this script

avatar image
0

Answer by NivekJump · Oct 20, 2014 at 06:12 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JA_555 · Oct 21, 2014 at 04:36 AM 0
Share

Thanks, I tried that and it has not worked

avatar image
0

Answer by _Yash_ · Oct 21, 2014 at 05:36 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JA_555 · Oct 21, 2014 at 08:12 PM 0
Share

Yep, the object is enabled in the hierarchy, since it is an important part of this scene.

avatar image
0

Answer by g8minhquan · Oct 21, 2014 at 06:44 AM

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"

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JA_555 · Oct 21, 2014 at 08:13 PM 0
Share

Yes it is named exactly as it is in the hierachy

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AddComponent not working 3 Answers

Adding a component in the editor with a script 0 Answers

GO.AddComponent just adds an empty Script 1 Answer

AddComponent deletes the game object. 1 Answer

C# - Disabled Script Still Runs! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges