@DMGregory You said "it looks up which key that was, then calls PressKey with that information". To do that, wouldn't I use like "If the Ray hit "Qkey", call PressKey(Q)"? And then as you instructed, have the keyboard script that detects the PressKey() call and then makes a char called '\x0051', and then takes that char and any new chars and combines them into a script, which is then displayed?
There are a couple of ways you could do it. One would be to make a Key script that just serves as a data container for a key code. When you fire your ray and get the collider the ray hit, you check if that collider has a Key script attached. If so, you read its key code variable.
Another way would be for your Keyboard script to hold a list of key codes and the collider associated with each one. Then when it gets its raycast result, it can look up the matching key code in that list (you can convert this to a dictionary to make that lookup easy)
You could even just use the names of the game objects that hold each collider. Name the Q key's collider object "Q" and then you can just grab raycastHit.collider.name as a string you can use. A little hacky, but simple.
Remember, there's a difference between an instance of a type and the type as a concept.
What you are doing is asking the concept of raycast hits "did you hit anything?" - and it cannot answer you, because it's just the idea of "hits" as a category of thing.
What you need is to get an actual instance of a raycast hit - one specific impact that actually happened - from a ray that you fired with eg. Physics.Raycast()
Whenever you see "An object reference is required for the non-static..." that means you need an actual specific thing here (instance), not the category of thing (type).
Continuing the discussion of types vs instances, in C# naming conventions, types get names that start with an uppercase letter (like MonoBehaviour or Collider or GameObject), so that makes it easy to see when you're referencing a type instead of a variable that's storing an instance (which usually start with a lowercase letter or underscore)
Yes! Now it worked! All that's left is "The name 'PressKey' does not exist in the current context" which s fine because I was going to do as you said, which was to "have something like a Typing script that exposes a method like public void PressKey(KeyCode key)"
You can save yourself some grief with something like System.Enum.Parse(typeof(KeyCode), keyName) so you don't need 50 different if clauses to convert your strings into key codes. Or you could just pass the string through directly.
Just note that if you use the parse approach, the object names need to match the keycode enumeration names exactly. (So, uppercase Q)
Okay. Last dumb question: How do I take the PressKey(KeyCode code) and turn the (KeyCode code) part into whatever letter or symbol that (KeyCode code) is?
Basically how do I pull what symbol/letter KeyCode code is?
Now how do I store the previously hit key? I know my final product should be someText = someText + newText, but how do I tell it that when the new "code" replaces the old "code", turn the old "code" into someText?
Right, because once again, declaring variables is your job.
They don't get magically declared for you. If you want someText as a member variable, it's up to you to write private string someText; inside your class.
Alright. But now all it tells me is that there is a } expected.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Typing : MonoBehaviour {
public void PressKey(KeyCode code)
{
someText = someText + code.ToString();
private string someText;
}
}
There's my code. I don't understand where it wants a "}"
I know I'm a broken record at this point, but none of this is stuff you need a game developer to tell you. This is bare bones intro-to-C# material you can find detailed tutorials for all over the net.
I know it’s a lot, but they really helped me get a better understanding of what I was working with.
@Wasabi Don’t worry though, just take your time and it will all come together. 😉
@DMGregory The funny thing is, I had no idea that there were different ways numbers or strings were stored a couple of months ago. I guess that means I’m getting better.