• 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 Fuuj1n222 · Dec 28, 2012 at 10:53 PM · gameobjectvariablegetcomponentscriptingbasics

updating a gameobject variable from another script attached to another object

Hi all,

I've seen many posts about this subject but none of these seem to really help. I've been stuck with this for 2 days now...

I have a very simple script named "intersection.js" that is attached to many GameObjects (like the plane named "A1" that is refered to lower) :

All it contains is the following, before functions start and update that are currently empty.

var busy : GameObject;
var back : GameObject;
var front : GameObject;
var left : GameObject;
var right : GameObject;

I want to assign to the variable "busy" a gameobject that this other script (TestMove.js) is tied to :

TestMove.js goes like this:

var targetObj : GameObject;
var targetComp : Component;

function Start () {
targetObj = GameObject.Find("A1");
targetComp = targetObj.GetComponent(intersection);

transform.position = targetObj.transform.position;
targetComp.busy = this; // <--- the problematic line...

}

Then I always get this error message...

Assets/TestMove.js(12,12): BCE0019: 'busy' is not a member of 'UnityEngine.Component'.

Please help a n00b trying to start coding :P

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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by NickCh · Dec 29, 2012 at 01:41 AM

The way you assign the script is wrong.

function Start(){

 var targetObj : GameObject = GameObject.Find("A1");
 var targetComp : YourScript =targetObj.GetComponent(YourScript);
 
 yourScript.busy = /*whatever you wanna do here*/ ;
 
 }
Comment

People who like this

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

Answer by infinitypbr · Dec 28, 2012 at 11:15 PM

I'm not sure about all your code, unfortunately.

But, if you want to assign a GameObject to the variable you've set up, you should be able to just drag-and-drop it in Unity. Otherwise, you could do something like the findbytag option to find it on Start()

Comment

People who like this

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

Answer by Fuuj1n222 · Dec 29, 2012 at 12:23 AM

Drag & drop works fine, but I need to do it with the script... I have no problem finding the object with GameObject.Find, or getting to the right component using GetComponent()... my problem is accessing/updating the variable.

Anyone have an idea? I know variables are public by default in Unity Javascript, so this shouldn't be the problem. (I tried adding public in front but with no more success)

Is there something special with gameobject variables?

Comment

People who like this

0 Show 3 · 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 infinitypbr · Dec 29, 2012 at 12:28 AM 0
Share

targetObj.GetComponent(intersection).variableName

The game object . GetComponent(script name) . variable you want to access

avatar image Fuuj1n222 · Dec 29, 2012 at 01:42 AM 0
Share

This is what I do when trying to access the variable named busy but I always get this same errror msg.

avatar image Fuuj1n222 · Dec 29, 2012 at 01:54 AM 0
Share

Finally I made it work!!!!

targetObj.GetComponent(intersection).busy = this.gameObject;

It's crazy,... I thought I tried this before with no success...

Thank you

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

10 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

Related Questions

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

Accessing Variables within another gameobject's script 2 Answers

Create a variable on a GameObject for access via the Object without GetComponent() 1 Answer

Accessing variables in another script using GetComponent() 0 Answers

Access collider custom script's variables 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