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
-1
Question by VenomRift · Jun 29, 2014 at 04:52 AM · getcomponent

Accessing Scripts from a different Script

So I've been trying to alter the contents of my "MoveRight" script from my "SurfaceDeathScript", specifically I want to alter the value of a variable, and I know I'm supposed to use the "GetComponent" thing to accomplish this. I've looked up several sources, but for some reason I can't seem to get it right. I'm using C# btw.

 public class SurfaceDeathScript : MonoBehaviour 
 {
  
     public MoveRight moveRightScript;
 
     void Start ()
     {
         moveRightScript = gameObject.GetComponent<MoveRight>();

I've tried this, but I get "the name MoveRight does not exist in the current context". I've also tried a whole manner of different ways, but to no avail. Any help would be appreciated, thank you.

EDIT:

Actually, I managed to write this and it worked:

 public class SurfaceDeathScript : MonoBehaviour 
 {
     public GameObject LevelScroller;
     public MoveRight moveRightScript;
  
     void Start ()
     {
         moveRightScript = LevelScroller.GetComponent<MoveRight>();
     }
  
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Player")
         {
             moveRightScript.levelscrollSpeed = 0.04f;
         }
     }
 }

In unity I dragged the LevelScroller object into both the LevelScroller and MoveRightScript boxes, as it contains the MoveRight script, while this does work, I get an error at the start that says "UnassignedReferenceException: the variable LevelScroller of SurfaceDeathScript has not been assigned".

Comment
Add comment · Show 3
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 rohitanshu · Jun 29, 2014 at 05:30 AM 0
Share

You have to assign a gameobject or a prefab to the LevelScroller in the inspector.

avatar image VenomRift · Jun 29, 2014 at 05:48 AM 0
Share

I placed the LevelScroller object which has the $$anonymous$$oveRight script onto an "enemy" object which has the SurfaceDeathScript. It ends up removing itself upon runtime though. I've removed the "public GameObject LevelScroller;" line though as I don't think it was needed.

avatar image Lo0NuhtiK · Jun 29, 2014 at 06:08 AM 1
Share

How To : Access Other Scripts

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by tswalk · Jun 29, 2014 at 05:23 AM

Like the error says.. you haven't actually assigned it anything as you have only created the variable to hold the reference, but not given it anything to reference (if that makes sense).

If all of these scripts are on the same object.. you were close in your first example as you "referenced" the "gameobject" to get its' component MoveRight.

see the example in the docs: http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

create the variable to hold the reference (in that case a public, so you can drag your object onto the variable in the inspector).

"get" the component (script) on that reference (which is basically itself in this example).

assign the value to that references variable.

and btw, you don't create a variable reference of the "class" you will use (moveright), you will create a reference for a GameObject (the object with the component you want)

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 Kiwasi · Jun 29, 2014 at 06:39 AM 0
Share

Nothing wrong with creating a variable of the class type reference to the component. Its far better to do it this way then to call getComponent every time you want it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Simple question about OnMouseButtonAsUp 1 Answer

Any problems with this script? 1 Answer

How do I select a joint component from a GameObject that has multiple joint components? 1 Answer

Implementing Setter/Getter 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