playMaker

Author Topic: Reading variables of clones [SOLVED]  (Read 7741 times)

bizilux

  • Full Member
  • ***
  • Posts: 171
Reading variables of clones [SOLVED]
« on: February 18, 2014, 02:06:31 PM »
ok so im having a problem with reading variables from instantiated prefabs.

basically im making bus simulator... and in main menu, player can chose which bus he will drive, lets say he chooses School Bus.

so when level 1 is loaded, one FSM instantiates chosen bus using Create object action.
i store that object into global gameobject variable called "Selected Vehicle"


this is how prefab of School bus looks like... i want to get a reading of Speed MS. it is a Float variable, inside Speedometer component (script)


i tried using get property, or get component, or set object value... nothing works...



get component should have worked... but the problem is this:
i cant select Speed MS anywhere... i can select component Speedometer, but i cant for the love of god select that i want to get float variable Speed MS of that speedometer component...
« Last Edit: February 20, 2014, 05:34:36 PM by bizilux »

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Reading variables of clones (instantiated prefabs)
« Reply #1 on: February 18, 2014, 02:47:24 PM »
is SpeedMS a public float?
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

bizilux

  • Full Member
  • ***
  • Posts: 171
Re: Reading variables of clones (instantiated prefabs)
« Reply #2 on: February 18, 2014, 02:56:50 PM »
i believe so...

this is the whole script... quite straightforward

i believe it also has extra code to it that i could delete... but that doesnt matter for now

Code: [Select]
using UnityEngine;
using System.Collections;

public class Speedometer : MonoBehaviour
{

private CarCameras m_CarCameras;
private CarSound m_CarSound;
private CarControl m_Car;
private CarControl m_SecondaryCar;
private CarControl m_CarControl;
private GameObject m_CarReset;
static private float GEARSPEEDMIN = 0.2f;

public float SpeedMS;
public string carGear;


void Start () {
GameObject m_Car = gameObject;
m_CarReset = gameObject;
m_CarSound = this.GetComponent <CarSound>() as CarSound;
m_CarControl = this.GetComponent <CarControl>();
}


void OnGUI ()
{




SpeedMS = new Vector2 (Vector3.Dot (gameObject.rigidbody.velocity, gameObject.transform.forward), Vector3.Dot (gameObject.rigidbody.velocity, gameObject.transform.right)).magnitude;



int gear;
if (m_CarSound) {
gear = m_CarSound.currentGear;
carGear = gear > 0 ? gear.ToString () : gear < 0 ? "R" : "N";
} else {
gear = m_CarControl.gearInput;
carGear = m_CarControl.getGear ();
}

}
}

1982

  • Junior Playmaker
  • **
  • Posts: 71
Re: Reading variables of clones (instantiated prefabs)
« Reply #3 on: February 18, 2014, 02:58:36 PM »
I work also with UnityCar Pro, and I know some variables cannot be pulled out. You can always attach new FSM to your Bus gameobject and use PM action "Get Speed". It gives you meters per second value, so you need to do little math to get kmh or mph.

bizilux

  • Full Member
  • ***
  • Posts: 171
Re: Reading variables of clones (instantiated prefabs)
« Reply #4 on: February 18, 2014, 03:12:07 PM »
I work also with UnityCar Pro, and I know some variables cannot be pulled out. You can always attach new FSM to your Bus gameobject and use PM action "Get Speed". It gives you meters per second value, so you need to do little math to get kmh or mph.

im using edy's physics, but yeh i understand what ur saying...

but i would still rather use my way... because i've come up with problems all the time on how to pull information out of Clones... so it is not just speed related problem...

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Reading variables of clones (instantiated prefabs)
« Reply #5 on: February 18, 2014, 03:31:47 PM »
Its public, you should be able to pull it out. It doesn't matter if it is a clone or not.

When you use Get Component, you choose a gameobject target and a variable target. The variable is the script title (Speedometer) in that big list.

After storing the component in that variable you use Get Property, target that variable(script) and pick the SpeedMS from the list.

Is it not there when you do that?
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

bizilux

  • Full Member
  • ***
  • Posts: 171
Re: Reading variables of clones (instantiated prefabs)
« Reply #6 on: February 18, 2014, 03:49:28 PM »
it is there... but it says "None" at speedometer component...



Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Reading variables of clones (instantiated prefabs)
« Reply #7 on: February 18, 2014, 04:01:43 PM »
*edit 'doh I see so its not storing the component.

Is this screenshot from runtime?
« Last Edit: February 18, 2014, 04:04:57 PM by Lane »
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

bizilux

  • Full Member
  • ***
  • Posts: 171
Re: Reading variables of clones (instantiated prefabs)
« Reply #8 on: February 18, 2014, 04:15:26 PM »
yes, the top screenshot, the one a bit greyed out is from debugging... basically i paused the game and debugged in that state. so yes its runtime... its not storing component yes

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Reading variables of clones (instantiated prefabs)
« Reply #9 on: February 18, 2014, 04:26:35 PM »
Try only getting once, after creating the object. (the component isnt changing, the variable is)

If that doesnt improve things then do a local test by putting the bus in the scene and getting the property by drag-n-drop. If still nothing, I'll look into it later tonight.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

bizilux

  • Full Member
  • ***
  • Posts: 171
Re: Reading variables of clones (instantiated prefabs)
« Reply #10 on: February 18, 2014, 04:35:10 PM »
every frame off, doesnt change anything, still at none


im not 100% sure what you mean, with getting the property by drag n drop?...
do you mean this?    if not, then explain a bit more please...


this works without problems... now im trying to make whole game scalable, so that i will have more busses, and that means dynamically spawn them, not have them in the scene, so yep thats where the problem is right now as you see...

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Reading variables of clones (instantiated prefabs)
« Reply #11 on: February 18, 2014, 04:59:32 PM »
Just made a quick test.. Getting components (and properties within them) from objects created as global variables at runtime and stored into a local variable seems to be working.

Are you using the latest version of PlayMaker?
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

bizilux

  • Full Member
  • ***
  • Posts: 171
Re: Reading variables of clones (instantiated prefabs)
« Reply #12 on: February 18, 2014, 06:25:48 PM »
1.7.6.f4  ... i can update to latest one, but i doubt it will change anything, im having problems with this for a long time^^

bizilux

  • Full Member
  • ***
  • Posts: 171
Re: Reading variables of clones (instantiated prefabs)
« Reply #13 on: February 20, 2014, 05:34:12 PM »
i found the problem.

i had to put wait action in... and put time to like 0.1 seconds... because this state was reached faster than bus was instantiated, so therefore, no component.

redikann

  • Full Member
  • ***
  • Posts: 174
Re: Reading variables of clones [SOLVED]
« Reply #14 on: February 20, 2014, 10:57:29 PM »
Yeah I've found if you try so get a reference Var to an instantiated prefab with"Find GameObject" in the Start state it would misfire, but if you used "Get Owner" it seemed reliable.