I have been trying to GetComponent of another GameObject inorder to call a method, I've tried everything I could think of but no sucess. I have 2 primitive spheres and I just want to call a method in Sphere2 from Sphere1. This is what I have now: var someObject : GameObject; function Update () { someO= someObject.Find("Player"); print("someObject name: " + someO.name); someO.GetComponent(MoveSphere).DoSomething(); } Sphere2 I tagged with the name: Player it doesn't print someO.name and it gives me a NullReferenceException for the last line. Can't you call another objects method?
First off, welcome to the forums! Before answering, a quick tip: get comfy with using the [ code ]...[ /code ] blocks in your posts (remove those spaces) as it lets you format code a bit more nicely. I'll edit your post when I'm done so you can look at that as an example. Of course you can call methods on other objects A few points/questions: 1. What exactly is someObject and have you assigned a value to that variable (via the Inspector) before testing this script? 2. When calling Find() you can just use GameObject.Find(...), no need to use a current game object reference. 3. If you're not seeing the name appear for someO.name then comment out the other code and sort things out. Perhaps you can error check the Find() return value against null to make sure it's finding something of that name. Here's my modified version of your code for testing: Code (csharp): function Update () { someO = GameObject.Find("Player"); if (someO == null) { print("No object found with that name!"); } else { print("someObject name: " + someO.name); someO.GetComponent(MoveSphere).DoSomething(); } } So step one is to verify that Find() is returning you something other than null. Make sure the name is identical, like look for leading/trailing space characters as you might use "Player" in the code above, but that will fail if the real object's name is " Player" (notice the space).
I just figured it out before I looked back at the forum. I changed someObject.Find("Player") to Find("Sphere2") but in my frustration with earlier code I had actually taken out of the MoveSphere script and had put it in its own script, but putting it back of course worked. Sorry beginer's unluck. Thanks for your reply though.
hey, rather than start a new topic... this fits in with me Have similar problem Code (csharp): GameObject go = GameObject.Find("GameManager"); print(go.name); go.GetComponent(GameManagerScript).AdjustScore(true); Now, I'm assuming it finds it, because it prints the name ok.. but then next line (GetComponent) returns compile error CS0119 If I put it in quotes, so: Code (csharp): go.GetComponent("GameManagerScript").AdjustScore(true); Then compile error 'UnityEngine.Component' does not contain a definition for 'AdjustScore' The name of the script is GameManagerScript.cs, when dragged onto a GameObject its component is called Game Manager Script (Script) (and for futility's sake I tried every combo!) ...and it contains a public function called AdjustScore(bool myBool) What have I missed!
Hey i just had the same problem. I'm new to c sharp, but I think what you are looking for is: Code (csharp): go = GameObject.Find("GameManager"); goScript = (GameManagerScript)go.GetComponent(typeof(GameManagerScript)); goScript.doSomething(); why do I always have to cast the scripts?
Just to provide some background: the reason the above works is that you have to specify the type when using GetComponent(). Without doing that the generic data type returned is Component and that's the base class without your custom functions. If instead you type the return value to your script then it can find your public functions/variables and not produce script errors.
This works great if you're calling another C# script, but I can't find the right incantation for calling Javascript from C#. Says the type is undefined. For example, Rotator is an object with Rotator.js attached, and Unity says it is undefined: Code (csharp): GetComponent(typeof(Rotator))).rotateRate.z = 3;
HiggyB, thanks very much for these kinds of posts, as it's really helpful to know the "why" . However, in this case your explanation is not crystal clear to me. Are you saying that the above typecasting is the proper way, or not? You say Do you mean without specifying the type? As far as I can tell, GetComponent requires a Type, and always returns a Component. So doing this in C# requires typecasting the result, yes? A more concise version of the above which worked for me (if you don't need to retain the Object reference).... Code (csharp): public GameManagerScript goScript; void Start() { goScript = (GameManagerScript)GameObject.Find("GameManager").GetComponent(typeof(GameManagerScript)); } void Update() { goScript.doSomething(); }
Proper depends on your desired goal. If all you want is a reference to it as a generic Component then you do not need to specifically type cast I'd guess. With that in mind that's almost never really the case and therefore the "proper" way is to type cast as you almost always want access to the script, its specific variables and of course methods. If you want access to your script component's members (properties, methods) as opposed to only the base class component's members then yes (which is like 99.99% of the time).
I implemented generic versions for the next version of Unity, for the c# users in need of some lovin'. Animation a = GetComponent<Animation>(); note the lack of casting, compiler verified typesafety, and reduced amount of typing. Same for AddComponent<Animation>(), FindObjectsOfType<Whatever>(), and friends. If you can't wait, what I used to do myself is create a MyMonoBehaviour class that implements all these helper functions. Bye, Lucas
Don't use that anymore! As Lucas Meijer_old also posted 11 years ago there are generic version and you can/should just do Code (CSharp): goScript = go.GetComponent<GameManagerScript>(); goScript.doSomething(); or simply directly use Code (CSharp): goScript = FindObjectOfType<GameManagerScript>();