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 jamespaxi · Jul 22, 2013 at 12:40 PM · c#componentaccessing from any scriptanother script

Script can't find Component within Start() - C#

Hi folks,

I'm trying to get another script `Script2` via `GetComponent()` within the `Start()` method of Script1.

Unfortunately it return nothings at times and the script fails. I'm told that the variable isn't instantiated.


I tried moving the Script1 lower on the Component list of the GameObject, but it still turns up problems at times.


Is this because when Start() is called Script2 doesn't exist yet? Or something along those lines?

Really not sure what to do here.

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 KiraSensei · Jul 22, 2013 at 02:04 PM 0
Share

If I understand correctly, the component you are looking at and the script calling GetComponent are attached to the same game object ?

avatar image andytouch · Jul 22, 2013 at 02:07 PM 0
Share

Could you post your code please? We might be able to understand your problem a bit better!

avatar image robertbu · Jul 22, 2013 at 02:19 PM 0
Share

I'm assu$$anonymous$$g that Script2 is on another game object? If so, how does that game object get created?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by IgorAherne · Jul 22, 2013 at 02:29 PM

1) Create an empty GameObject. Name it _second

2) Drag Script 2 onto the _second in the hierarchy view

3) Create an empty GameObject. Name it _first

3)Drag Script 1 onto the _first in hierarchy

in Script1 declare a variable

  var shortcutToScript2 : Script2;

4)In start() of Script 1 write:

  function Start(){
         shortcutToScript2  = GameObject.Find("_second").GetComponent(Script2);
     }
 
 //now whenever you want to reference Script2 (That particular instance in the gameObject _second) from Script1, simply use shortcutToScript2
 
 //for example
 function Update(){
      shortcutToScript2.myVariableOfScript2++;
 }

if you need to reference a component in the same game object, say you have both Script1 and Script2 on _first. Then you could create a shortcut to script2 from script1 like this:

 shortcutToScript2 = this.GetComponent(Script2)

   
Comment
Add comment · Show 2 · 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 aldonaletto · Jul 22, 2013 at 02:33 PM 0
Share

You should declare shortcutToScript2 outside any function, and assign to it at Start. If declared inside a function, the variable is temporary and will exist only while Start is executing.

avatar image IgorAherne · Jul 22, 2013 at 02:36 PM 0
Share

did i? :D

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

19 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

Related Questions

AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers

Singleton code and Unify down. 1 Answer

Null Reference Exception on instantiated object's script. 2 Answers

Decrease life from another script 1 Answer

Multi similar script Components same game object 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