How to call a method from a class in a different script in c#
I am working on a final for my Video Game Design course, and I am trying to make a double door slide open when a pressure plate is activated.
The pressure plate sctrip is
and the door is
the DoorLeft and DoorRight scripts are identical, but i can only upload two images. The only difference is that every instance of the word "Left" is replaced with "Right". The numbers in the door script are currently placeholders.
When I try to run the program, I get the following error "an object reference is required for the nonstatic field method or property 'UnityEngine.Component.Transform.get'"
Does anyone know how to get this to work?
$$anonymous$$ay you please explain what you mean? I am new to the Unity Forums, so I don't quite understand what you meant. Thank you.
Answer by Jason2014 · Jun 23, 2017 at 09:50 AM
You have to remove "static" modifier from both functions and create instances of that doors in the beginning of script "Pressure Plate". Type this after "public class [...]" line:
public DoorLeft doorLeft;
public DoorRight doorRight;
Attach components (in Unity script is also component) to slots which will be available after declaring public variables. Then in "OnTriggerEnter" function before method call type "doorRight.DoorRightOpen ();" and so on. It should work.
Your answer
Follow this Question
Related Questions
Method not being called from another script 1 Answer
Use a class like a function 1 Answer
Why i cant call this simple method?(c#) 1 Answer
When should I use static fields/methods? 2 Answers