playMaker

Author Topic: Set variable on a script on prefab instanced at runtime.  (Read 270 times)

Aaddiction

  • Full Member
  • ***
  • Posts: 108
    • View Profile
Set variable on a script on prefab instanced at runtime.
« on: May 01, 2020, 11:20:25 AM »
Hello,

I'd like to set a variable on a script attached to a game object that is instantiated at runtime. How to do that? It's easy if I have the object at the scene and it's only one object, but I want to be able to set a script on a different object with the same FSM.

Example:
car1 - prefab with script name "car engine" with tag "car"
car2 - prefab with script name "car engine" with tag "car"
car3 - prefab with script name "car engine" with tag "car"

If I drag the "car engine" script from car1, which I put in the scene to an FSM called "car throttle" I can easily set the car throttle variable at run time for this car1.

But I want to access this car throttle variable to the other cars as well with single FSM in the scene.

So I want to do the following:

1. Find game object with tag "car".
2. Store it in a player-car game object.
3. Set car throttle variable in the player car game object in the script "car engine".

How to do point 3 at runtime, given that I can choose any car?

Thank you!

ch1ky3n

  • Full Member
  • ***
  • Posts: 117
    • View Profile
Re: Set variable on a script on prefab instanced at runtime.
« Reply #1 on: May 01, 2020, 11:34:13 AM »
Are you familiar with send event ?
you could send global event with broadcast message
then you make global event on each car as a trigger.

anyway here what you should try

1. Make a new game object and add FSM on it
2. Use "Set Event Data" and set Float or int a variable that contain the value you want to assign on your cars
3. use "Send Event" > Broadcast Message and give it name "CHANGE YOU ALL" or whatever
4. then on the prefab, right click on empty space add Global Transition "CHANGE YOU ALL"
5. on the state that you just created, retrieve the value by using "Get event Float Data" and save it to variable (Use int if your value is int)
6. Assigned those retrieve value to the throttle variable

it should change!

Aaddiction

  • Full Member
  • ***
  • Posts: 108
    • View Profile
Re: Set variable on a script on prefab instanced at runtime.
« Reply #2 on: May 01, 2020, 12:59:53 PM »
Thanks a lot, but may be I didn't give the best example.

I also want to get specific information from the currently selected car, like rpm and gear, so it's not only about sending. I want to to receive information without having to attach the same FSM over and over again to each car so I can reference the script on each car.

I want to find this script during runtime and having the control actions on game manger FSM that finds the selected car and that script.
« Last Edit: May 01, 2020, 01:02:03 PM by Aaddiction »

ch1ky3n

  • Full Member
  • ***
  • Posts: 117
    • View Profile
Re: Set variable on a script on prefab instanced at runtime.
« Reply #3 on: May 01, 2020, 08:30:15 PM »
Have you tried to set up arrays to received all the information from your car prefabs?
So the idea is to make all the cars send its information to arrays once they spawned.
thus you can retrieve each car information from the arrays.

you could also save each car gameobject on array type gameobject if you want to call some method on the script.

cheers