Can't reference script of parent
Hi guys,
I want to call a function in the parent but I get a 'The name does not exist in the current context' error.
EDIT: Error message
Assets/Scripts/LaneDetection.cs(14,74): error CS0246: The type or namespace name `Platformer2DUserControl' could not be found.
EDIT2: corrected
transform.parent.gameObject.GetComponent<Platformer2DUserControl>()
Tried several variations but nothing works.
Nothing found during research.
Please help me.
Does gameObject.GetComponentInParent<Platfomer2DUserControl>()
work?
$$anonymous$$aybe just a typo, missing 'r'? "Platfo(r)mer2DUserControl"
doublemax and Glurth are most likely right. Next time when you post a question about a compiler error include the whole error message and the actual wording.
You can select and copy text directly from the detail area of the console window in Unity. No need to type the error manually.
Answer by Glurth · Sep 05, 2016 at 04:11 PM
'The name does not exist in the current context'
The error means that one of the identifiers (names) in the line either has not been defined, or is "out of scope". I would suspect Platfomer2DUserControl is the culprit identifier (as doublemax mentioned), because I recognize the others. I don't remember the last time I actually TYPED in a class name, far too error prone (without visual studio intellisence). I usual copy and paste class and variable names, to avoid just such errors.
Such an issue can also occur, even with properly spelled class names, if the class name in questions is "out of scope". Usually this has to do with "namespaces", but unless you have defined your own, this is probably not the issue in this case. If you HAVE defined your own namespace, and within it's scope is Platfomer2DUserControl: THEN you will need to include a "using" command at the top of your script specifying that namespace. This command will put that namespace "in scope" for that file.
The spelling was my mistake, sorry. Nonetheless it won't autocomplete my script name. Added error message in question. Please explain the namespace topic I think that's what I need.
I have found I need to recompile the script successfully, AFTER I add "`using someNamespace;`" to the top of the file: at that point intellisence can finally see into the namespace, and make suggestions.
Answer by thecakeisalie · Sep 05, 2016 at 07:15 PM
Thanks to @Glurth, I added the same namespace and took @BeckStarDEs solution and now it works.
Thanks!!