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 sdgd · Jan 19, 2013 at 06:20 AM · getcomponentreplacemanual

can someone Repair Manual GetComponent Please

in GetComponent

I found this thread in 3 hours searching through all Q/A about accessing the variable in other scripts

http://answers.unity3d.com/questions/141814/im-having-more-problems-accessing-a-variable-from.html

it will not only help new players it'll help me too as I'll propably forgot about it and want to search for it in future

and please don't say to people on most frequently asked questions to RTFM because maybe the man doesn't describe that problem correctly

if someone has better example of code please share it to help our wiki

please add the code

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour {
     public OtherScript sscript;
     public GameObject ObjectWithScript;
     void Start() {
         sscript someScript;
         ObjectWithScript = GameObject.Find("Player");
         sscript = ObjectWithScript.GetComponent("OtherScript") as OtherScript;
     }
     void Example() {
         Debug.Log (OtherScript.x);
     }
 }




 using UnityEngine;
 using System.Collections;
 
 
 public class OtherScript : MonoBehaviour {
        public int x = 10;
Comment
Add comment · Show 4
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 Lachee1 · Jan 19, 2013 at 06:41 AM 0
Share

What about the code in the scripting reference, it it pretty good in my eyes

avatar image sdgd · Jan 19, 2013 at 11:47 AM 0
Share

no lol

it does not contain sscript = ObjectWithScript.GetComponent("OtherScript") as OtherScript;

it never tell us to find GameObject First and it's totally incorrect for searching values in other scripts as what ever you do you'll get null reference untill you add GameObject to it and that's only explained in maybe 5% of all Q/A and even if it is it's not in all languages

avatar image Scribe · Jan 19, 2013 at 12:11 PM 0
Share

Quote from the script refernce for GameObject.GetComponent for C#

ScriptName other = gameObject.GetComponent();

surely having "gameObject" in the code line would prompt you to first add the correct GameObject...

Also the fact that the script reference entry is titled GameObject.GetComponent would hint toward it recieving a gameobject type to search for a component?

Scribe

P.S sorry for all the edits, couldn't get it to show up the around the ScriptName

avatar image sdgd · Jan 20, 2013 at 05:02 PM 0
Share

well actually in GetComponent it says only

ScriptName other = GetComponent("ScriptName") as ScriptName ;

http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponent.html

I know I saw the code you described above but never got it to work tho so I went on first working code

code not working = I don't understand how it should work || it was easier to my eyes and never got in to it

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by whydoidoit · Jan 19, 2013 at 12:27 PM

The manual has great examples of accessing other components - clearly the documentation for GameObject.GetComponent isn't really the right place for it so they put it here. There are additional tutorials and docs available for GetComponent on Unity Gems

Comment
Add comment · Show 4 · 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 sdgd · Jan 19, 2013 at 12:37 PM 0
Share

hmmmmm I've run through that page for more than 30 times yesterday and haven't figured out that is for that

thanks for telling me tho

but still I think it somehow needs some tweaks so new people could get it

avatar image whydoidoit · Jan 19, 2013 at 12:40 PM 1
Share

Well it shows on that page what you need to do (albeit using Transform.Find) but that's a good way of doing it too (you can limit the search for the children of an known item which makes it faster). I do agree that the manual is very limited in places and sometimes hard to navigate. I guess Unity only has so much money for adding features and documenting and relies on us to pick up the pieces :)

avatar image sdgd · Jan 19, 2013 at 12:58 PM 0
Share

well it's no problem for me to pick up the piece after them but I'm not able to change anything

as in other wikis I'm able to change stuff to help my self and other people

avatar image whydoidoit · Jan 19, 2013 at 01:07 PM 1
Share

No you can't change the manual (it isn't a Wiki). You can change the Wiki. I mean pick up the pieces by answering questions here.

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

12 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

Related Questions

SOLVED - String replace % with " in C# 1 Answer

Replace Existing Component? 2 Answers

Replace static placeholders with the animated ones 0 Answers

Keep replaced materials of a Game Object even after ending the Play Mode C# 0 Answers

Refrencing Instantiated 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