Asii
March 25, 2013, 12:12pm
1
Hey so i wanted to start using C# instead of UnityScript and i’ve run into a problem with the getcomponent.
so i just wanted to make a simple script where the player get a value from another script. But doesn’t seem to work.
I must be doing something horribly wrong so please help
Look see = (Look)cameraObject.GetComponent("Look");
//Rotation
transform.rotation = Quaternion.Euler(0, see.yRotation, 0);
Asii
March 25, 2013, 12:26pm
3
you should know how many times i’ve tried that.
From what i see, this code should work, though using the class instead of string representation is more common.
Maybe the error is somewhere else?
Look see = (Look)cameraObject.GetComponent(typeof(Look));
In C# this is the part that confused me most too.
But, correct way of using find and getcomponent.
GameObject.Find(“ObjectName”).GetComponent()
GameObject Object1 = GameObject.Find(“name”)
Component Script1 = Object1.GetComponent()
Calling a function within the script: Script1.FunctionName()
Hope that’ll help you out as a few examples
Also be sure to check pages like:
They always have examples for Java, C# and Boo, just try clicking the button and all examples on that page should change
Dantus
March 25, 2013, 1:58pm
7
If you make sure to select the C# examples, it should be straight forward:
Look see = cameraObject.GetComponent<Look>();
1 Like
Asii
March 25, 2013, 6:50pm
8
Many thanks for all the answers. I think i’ve cracked it
I’m stuck with the same problem, whenever I use the pattern referred to in the docs:
ScriptEnemy enemyScript = hit.transform.GetComponent<ScriptEnemy>();
enemyScript.numberOfClicks -= 1;
I always get a compile error:
Which would actually seem correct since my scriptPlayer.cs contains no referrence to something of type ScriptPlayer, so what’s the correct form for this statement? The script attached to the object hit is called scriptEnemy.cs
I’ve even tried:
Component enemyScript = hit.transform.GetComponent<scriptEnemy>();
enemyScript.numberOfClicks -= 1;
Which gets rid of the CS0246 error but then the compiler tells me;
…back to square 1.
scriptEnemy enemyScript = hit.transform.GetComponent<scriptEnemy>();
Thank you KelsoMRK, that did the trick. I still can’t believe I didn’t try it like that.
Wow so many replies telling him to look at reference, am i the only one seeing this when looking at c# examples? “If you make sure to select the C# examples, it should be straight forward:” damn you…
I was looking for an answer aswell, have to keep looking i guess ;/
Dantus
February 16, 2014, 11:25am
13
Gravn:
Wow so many replies telling him to look at reference, am i the only one seeing this when looking at c# examples? “If you make sure to select the C# examples, it should be straight forward:” damn you…
I was looking for an answer aswell, have to keep looking i guess ;/
Welcome to the forum!
The C# example code existed at the time the question was asked. Even if the documentation is not there, what is not clear about the answer of KelsoMRK?
Annihlator:
In C# this is the part that confused me most too.
But, correct way of using find and getcomponent.
GameObject.Find(“ObjectName”).GetComponent()
GameObject Object1 = GameObject.Find(“name”)
Component Script1 = Object1.GetComponent()
Calling a function within the script: Script1.FunctionName()
Hope that’ll help you out as a few examples
Also be sure to check pages like:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
They always have examples for Java, C# and Boo, just try clicking the button and all examples on that page should change
Unity only supports C#, Boo, and Javascript. Java and Javascript are two totally different languages.
User340
December 6, 2014, 10:46pm
15
Weird, the string variant works just fine for me.
How do I reference a script that is not in the current solution? I need to figure out how to reference the CrossPlatformInputInitialize .cs script that enables editor capabilities. It exists in the Editor folder, which is technically within my project/solution, but I get the error message: The type or namespace name CrossPlatformInputInitialize could not be found. In other words, what do I replace this with? GetComponent();
You would need to tell compiler that you will be using that scope.
using UnityStandardAssets.CrossPlatformInput.Inspector;
Thanks OneDragutin, but I need to include this code in my script, because the following directive cannot be built to a standalone:
using UnityEditor;
So, I would like to know how I can make this script available to my code, so that I can still use editor stuff in a build…
Here is what I have written so far, and I just need to figure out what code to put instead of the commented line:
#if UNITY_EDITOR
GUILayout.BeginHorizontal( displayStyle1 );
GUILayout.Label( field.tagLine, GUILayout.Width( Screen.width/3 ) );
selGridInt = GUILayout.SelectionGrid( selGridInt, selStrings, 3, EditorStyles.radioButton );
GUILayout.EndHorizontal();
inputs[i] = selStrings[ selGridInt ];
#elif UNITY_STANDALONE
string standalonePath = Application.streamingAssetsPath;
string editorPath = standalonePath + Path.DirectorySeparatorChar + "Editor";
// What goes here? Something to do with GetComponent<CrossPlatformInputInitialize>()?
GUILayout.BeginHorizontal( displayStyle1 );
GUILayout.Label( field.tagLine, GUILayout.Width( Screen.width/3 ) );
selGridInt = GUILayout.SelectionGrid( selGridInt, selStrings, 3, EditorStyles.radioButton );
GUILayout.EndHorizontal();
inputs[i] = selStrings[ selGridInt ];
Your suggestion is a directive. Do you mean it will build and make available what I need just like
using UnityEditor;
in the Editor mode?
Hi OneDragutin,
With your suggested directive, I still get an error message when I try to build:
The name ‘EditorStyles’ does not exist in the current context.
Is there any way I can reference the CrossPlatformInputInitialize .cs script in my build?
Thanks
Why are you trying to use editor functionality in a build of your game in the first place? What are you actually trying to do?
1 Like