- Home /
Common question. Search GetComponent
linking scripts
So i need my script linked right? E.G accessible by another script here is where i stumble, lets say i have a code like this: public class Example : MonoBehaviour {
public Component Example
void Start () {
Example = GetComponent<Example>();
}
}
and i want to link it to this script: public class Example1 : MonoBehaviour {
void Update () {
Example1 = GetComponent<Example>().enabled = false;
}
}
I cannot find out how to link these scripts together!
Use the Type ins$$anonymous$$d of 'Component'.
public Example myExample;
Answer by Lost_Syndicate · Feb 05, 2018 at 04:51 AM
I found out how. I had to change the scripts class to something else then change it back it its originall state. Fix this unity plss
That's not how program$$anonymous$$g works. The code you posted wouldn't even compile (unless you also have type called Example).
To explain what you need to do: Component is the base type of $$anonymous$$onoBehaviour, which is the base type of Example1. You can store a reference to a variable of a derived type in a variable of with the base type, but you will only be able to access the functionality of the base type.
So if your class Example1 has a function called DoSomething(), and you store it in Example (which is of type Component) using GetComponent(), then you will be able to access its property enabled, but not the function DoSomething().
As an advice, I would suggest that you learn the basics of C#.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Rotatearaound a moving object 1 Answer
help with lists inside lists 1 Answer