- Home /
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;
What about the code in the scripting reference, it it pretty good in my eyes
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
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
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
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
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
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 :)
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
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.