- Home /
how to create a custom type in C#? public Atype atype;
Ive seen other people make there own types in scripts like this..
private AnotherScript anotherScript;
private YetAnotherScript yetAnotherScript;
private BoxCollider boxCol;
yet when i try to do this i get errors that they arent real types. how can i make them usable types?
i want to do:
MyScript myscript = target.GetComponenet();
Firstly, you must make a new class in a new script file called $$anonymous$$yScript that has it's definition / declaration.
Secondly, I believe "GetComponent" method requires you inputting a type. I believe you'd want to do target.GetComponent($$anonymous$$yScript)? I'm not sure what you're trying to achieve, so I'm kind of just guessing.
You may need to provide more code. I'm not sure if you've even made a $$anonymous$$yScript class at this point, so it's hard for me to help you figure out why you can't have "usable types". Are you getting an error upon compilation? Is this an editor script you're working on? $$anonymous$$ore info would be nice before a finalized answer.
i have not made any new classes.
target.GetComponent($$anonymous$$yScript); was indeed what i wanted to do.
I need a method for calling a script in GetComponent, then using the variables in the script i called like so:
target.GetComponent(ScriptINeedToGet);
ScriptINeedToGet.VariableIWantToChange = NewValue;
Answer by CostelloNicho · Jan 30, 2014 at 11:27 AM
ScriptYouNeed script = target.GetComponent<ScriptYouNeed>();
script.VariableToChange = SomeValue;
or
target.GetComponenent<ScriptYouNeed>().VariableToChange = SomeValue;
int targetHp = target.GetComponenent(Player).playerHp;
this errored, any idea why? error log is CS1061: Type UnityEngine.GameObject' does not contain a definition for
GetComponent' and no extension method GetComponent' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
and i apologize, i marked this question as answered by mistake earlier, i undid that though, since i still do not understand.
i figured it out, i was trying to find a script of a seperate type.. int targetHp = target.GetComponenent (); should have been..
Player = target.GetComponenent ();
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Custom Collision Script (solved) 1 Answer
How to sort get components? 3 Answers
How to put specific shapes/textures instead of generating colors (C#) 1 Answer