-1
\$\begingroup\$

So, I know that there are better ways to access a variable on another object, but this way, as far as i know, needs to be accessed in this specific way. What I need to do is get a variable on an object when that object is hit by a ray. Something like GetComponent but for a variable. Ive already got the ray able to spawn and check if the object the ray hit has the script with the variable in it, i just need to access the variable.

I cant really set this up in the inspector, as the variable i need is on something that there are many of, so i need to get the variable from the specific object that was hit by the ray.

Ive been researching it for awhile but it seems all the other questions are either not what im looking for or just outdated. I dont really think posting any code would help in this case, as what i have currently does everything except what im asking about here.

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Something like GetComponent but for a variable

That's still just GetComponent. You'd use it like this:

if (Physics.Raycast(ray, out var hitInfo)) {
    if (hitInfo.collider.TryGetComponent(out SomeComponent comp)) {
        comp.variableToChange = "Success!";
    }
}

Here I chose to use the TryGetComponent flavour as it's a bit more concise in cases where the component might not be present, and avoids unnecessary memory allocations when using it in the editor (regular GetComponent has a special editor behaviour where it spawns a dummy object if no matching component was found, to help you get better error messages - but coding defensively with the Try version avoids that)

For this example, I'm assuming that the variable you want to read or assign is declared something like...

public class SomeComponent : MonoBehaviour {
    public string variableToChange;
}
\$\endgroup\$
2
  • \$\begingroup\$ This is what i was looking for, and it seems to work exactly as i need it to. What I was trying to do was to check if the thing that was hit by the ray was a piece of armor, and if it was, to test to see if the ray, being a projectile, had a high enough armor penetration to bypass it. I needed to get the "armorThickness" variable to see that though. But i dont think i could setup a reference manually through the inspector because there may be dozens of pieces of armor at any time, so i would need to get that component from a specific piece of armor as it is shot. \$\endgroup\$
    – Chuck
    Jul 7 at 15:27
  • \$\begingroup\$ You don't need to re-explain your question after you've gotten an answer. If you're concerned that your question was unclear, edit the question. \$\endgroup\$
    – DMGregory
    Jul 7 at 15:34
2
\$\begingroup\$

If I understand your question correctly, then you have multiple components of different classes which each have a variable float foo, and you want to access the foo of the first component on the object which has a foo variable.

You could solve this problem with interfaces.

Step 1: Define an interface as a new C# file. Let's name it IHasFoo.

interface IHasFoo() {
    public float GetFoo();
    public void SetFoo(float newValue);
}

Step 2: Make each class which has a foo variable implement that interface:

public class ThingamabobController: MonoBehaviour, IHasFoo {
    private float foo;

    public float GetFoo() { return foo; };
    public void SetFoo(float newValue) { foo = newValue };
}

Step 3: Now when you use GetComponent<IHasFoo>() on an object, you get a reference to the first component on the object which implements the IHasFoo interface. The return value is of the type IHasFoo, so you can only call the methods defined in that interface. But that's all you need.

var fooHavingComponent = otherObject.GetComponent<IHasFoo>();
if (fooHavingComponent != null) {
     fooHavingComponent.SetFoo(42.5f);
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ This could be done a bit more elegant with an interface defining a property instead of a classic getter and setter method, but I didn't want to introduce yet another syntax element of the C# language which the question author might not know about yet. \$\endgroup\$
    – Philipp
    Jul 7 at 9:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .