J
J
Jores_T2019-01-18 21:05:13
C++ / C#
Jores_T, 2019-01-18 21:05:13

How to pass a variable to a script attached to an object?

Good afternoon, I'm writing a platformer for learning, I'm a beginner myself.
Faced such a basic problem, I do not know how to solve it.
In general, I'm trying to make shooting at the character.
I made a prefab from an object with a sprite of a flying bullet, attached a script to it, in which I registered the velocity at the desired speed with the "right" vector.
A script is attached to the Player object, where the shot is processed - an instance of the bullet prefab is created at the desired point, after which it flies to the right at the desired speed. Made via Instantiate()
The task is this - when the player looks to the left to shoot to the left, now it is logical that the bullet flies only to the right.
I don't understand how, when creating an object via Instantiate(), to pass the boolean variable flipX to the script attached to this object. With its value 'true', I would put the velocity vector not "to the right", but "to the left". It is important for me to understand how this is implemented in principle, or in general it is necessary to implement it differently by changing the architecture.
There is a solution in the forehead - to make 2 prefabs, one for flying to the left, the second for flying to the right and create the right one depending on the direction of view. But I think it's kind of a clumsy solution.
Regarding the hierarchy - instances of the prefab spawn in the root, next to the Player. (I don't know if it matters)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Pogrebnyak, 2019-01-18
@Jores_T

GameObject GO = Instantiate(...);
GO.GetComponent<MyScript>().myVariable = ...;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question