Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Winston · Feb 22, 2011 at 02:36 PM · javascriptclassaccess

Accessing another script from a class

I am trying to access other variables and functions from inside a class I have created using Javascript. For example:

script 1:

private var x

 public function setx(newX)
 {
   this.x = newx;
 }

script 2:

class Class1 {

function doSomething() { script1.setx(1); } };

How would I go about achieving this?

Comment
Add comment
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
0

Answer by efge · Feb 22, 2011 at 02:42 PM

You could use the function GetComponent.
Look at the example in the reference.

Comment
Add comment · 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
0

Answer by Jessy · Feb 22, 2011 at 02:42 PM

You just need a reference to the instance script1.

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

For example:

private var x;

public function SetX (x) { this.x = x; }

.

class Class1 extends UnityEngine.Object {
    function DoSomething () {
        (FindObjectOfType(Script1) as Script1).SetX(1);
    }
}
Comment
Add comment · 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 Winston · Feb 22, 2011 at 02:54 PM 0
Share

I have tried looking at this but I can not work out the correct path to the object. If I just use GetComponent() I get an unknown identifier error because it in a class.

avatar image Winston · Feb 22, 2011 at 03:05 PM 0
Share

I get the same error "$$anonymous$$ Identifier" because something needs to come before FindObjectOfType. I have tried "Object" but that gives me another error "$$anonymous$$ethod not found". As is not recognised as a keyword. Is it C#?

avatar image Jessy · Feb 22, 2011 at 04:43 PM 0
Share

Sorry. I added in "extends UnityEngine.Object". That will solve it, but you'd be better off deriving from whatever you need, like $$anonymous$$onoBehaviour, perhaps. If you really don't need that, you can use UnityEngine.Object.FindObjectOfType.

avatar image
0

Answer by Winston · Feb 22, 2011 at 03:24 PM

I have found the answer. The reference link I needed to use was:

var Script1script = gameObject.GetComponent("script1");

Script1script.SetX(1);

Thanks for the help.

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 Jessy · Feb 22, 2011 at 05:41 PM 1
Share

There was no way for us to offer that, given that you did not mention that both scripts were attached to the same Game Object. GetComponent is also a function of Component. No need for "gameObject." But most importantly, don't use that form of GetComponent. (And use PascalCase for class names.) GetComponent.()

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Access variable from another script? Health! 3 Answers

Accessing function from another script won't work 1 Answer

How do I access a variable declared outside a class? 1 Answer

Can't get script to access other script/class (c#) 1 Answer

Using static constants from an outside script 2 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