• 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
Question by Clarinet · Mar 15, 2015 at 11:56 PM · instantiategetcomponent

How to activate a script function on an instantiated object?

Hi there, I would like to (through Javascript) activate a function on a script attached to an instantiated object via a different script, but I'm not exactly sure how to word it so the editor is happy. This is the part of the code I'm having trouble with:

 var z : Transform;
 
 function OnTriggerEnter(hit : Collider){
 var zz = Instantiate(z, Vector3(3032.3,2781.8,3068.6),Quaternion.identity);
 zz.gameObject.GetComponent(yesz);
 zz.CanWork=true;
 }

zz is instantiated from z which already has the script "yesz." I'm trying to make the "CanWork" variable for zz true.

The editor has a problem with the "zz.CanWork=true;" line, with the MissingFieldException of UnityEngine.GameObject.CanWork.

When I replace the "g" with "G" in "gameObject" in the line above that, the editor has a problem with that line and it says that the object reference is not set to a particular instance of an object.

Could someone please tell me what I'm doing wrong? Thank you.

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by dethtoll · Mar 16, 2015 at 08:23 AM

GetComponent returns an object of the specified type. Refer to the documentation

I suspect you want something like this (assuming yesz is the type of the script):

 zz.gameObject.GetComponent(yesz).CanWork = true;


Comment

People who like this

0 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 Clarinet · Mar 16, 2015 at 10:57 PM 0
Share

Thank you kindly. This has fixed my problem as well as NoseKills' answer!

avatar image

Answer by NoseKills · Mar 16, 2015 at 08:31 AM

Firstly, I think the compiler might get a bit confused because you are not defining a type for zz. That's not the main problem here but it's never a bad thing to define types to your variables. If nothing else, it helps you get better, more descriptive error messages when you do something wrong.

The real problem is exactly what the error says. The class GameObject does not have a variable called CanWork. That variable is in your script yesz where you put it. Since you are not defining the type of zz, the compiler seems to decide it must be a GameObject. Then you are trying to change CanWork on that GameObject.

You are calling GetComponent to get the yesz script on the GameObject but you don't do anything with the returned yesz script.

      var zz : Transform = Instantiate(z, Vector3(3032.3,2781.8,3068.6),Quaternion.identity);
      var yeszScript : yesz = zz.gameObject.GetComponent(yesz);
      yeszScript.CanWork = true;

Comment

People who like this

0 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 Clarinet · Mar 16, 2015 at 10:57 PM 1
Share

Ah okay I get what you are saying. Thank you for that insight! This script works along with the other one provided by dethtoll.

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

22 People are following this question.

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

Related Questions

Getting a component of an instantiated object returns an error 1 Answer

How do I acces a specific GameObject within the terrain???? 1 Answer

instantiate prefab and link 3rd party gameobject script 1 Answer

Instantiate Prefabs. Errors. 2 Answers

Only change a variable on the instaniated object not the prefab. 0 Answers


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