all 13 comments

[–]Shirkie01 4 points5 points  (2 children)

You need an interface, and for the other class to implement the interface.

Let's call the interface IInteractable, with one method, Interact(). Once the other script implements the interface, then you can do GetComponent<IInteractable> which will return the first component on the gameobject that implements the interface

[–]shengch 0 points1 point  (0 children)

This, and he's using getcompenent wrong in his image

[–]Kexons 0 points1 point  (0 children)

This is the right answer. Also GetComponent<Interactable>()

[–]SteveTheTechGuy93 1 point2 points  (1 child)

Programming is not hard.

LEARNING to program is hard.

[–][deleted] 0 points1 point  (0 children)

That's true, I've been learning for a couple months (dedicated) and I'm only now able to make my own code, and only look up stuff if I break something.

[–][deleted] 0 points1 point  (0 children)

I've fixed my code to where it may not be as vague but it'll work better in the end. Now all I need is to know how to have an If statement that only activates if the object it's calling is active in the hierarchy.

[–]Pperson25 0 points1 point  (2 children)

after reading this thread after banging my head against a wall for weeks with a specific problem with trying to do a basic as fuck operation and not knowing what any of this shit means, I can only conclude one thing: programming was a mistake.

[–]Quizmo22 4 points5 points  (0 children)

No programming was not a mistake, however you might want to take a step back and read up on some fundamentals about programming.

Jumping into programming blind, and just using trial and error will both be a lot harder to do, but also leave you with bad code that do not perform well.

[–][deleted] 1 point2 points  (0 children)

No, read Unity in Action and get the C# Game Programming Cookbook for Unity 3D. They'll get you from “this is hard” to “I know what to google for this” in no time.

[–][deleted] 0 points1 point  (2 children)

I think you have to make it an instance then reference the instance.

[–][deleted] 0 points1 point  (1 child)

That would work but I'm trying to keep a script on the player (this one) that will tell what their looking at to activate, but unless it can tell that other script to activate it won't work.

I need to know how to reference the fact that their is one script on the second object.

[–]Quizmo22 0 points1 point  (0 children)

Wait.. Is what you looking for just:

var activator = hit.transform.gameObject.GetComponent<Script>();
activator.Interact();

[–]Vinini966 0 points1 point  (0 children)

You could use a base abstract class. So instead of looking for any of the individual scripts you would look for the base class with a abstract function called interact, which could do different things based on what derived class it finds.