1

I have a script to position the player where fell off from the platform.

Platform Tracker.cs

public GameObject platformTrackerPoint;
public Vector3 platformTransform;

public PlayerMovement thePlayerMovement;

void Start()
{
    platformTrackerPoint = GameObject.Find("PlatformTrackerPoint");

    thePlayerMovement = FindObjectOfType<PlayerMovement>(); 
}

void Update()
{
    if ((transform.position.x < platformTrackerPoint.transform.position.x) && thePlayerMovement.grounded)
    {
        platformTransform = gameObject.transform.position;
        Debug.Log ("Plaform Transform =" + platformTransform);
    }
}

As I debugged, platformTransform shows the value I need. But its not reflecting in the code below.

GameController.cs

    public PlatformTracker thePlatformTracker;

    public Vector3 tempPosition;

void Start () {
        platformStartPoint = platformGenerator.position;
        playerStartPoint = thePlayer.transform.position;

        thePlatformTracker = FindObjectOfType<PlatformTracker>();
        thePlayer = FindObjectOfType<PlayerMovement>();
    }


    public void RespawnPlayer()
    {
       thePlayer.transform.position = thePlatformTracker.platformTransform;
    }

Your help is much appreciated. Please let me know if anything is not clear.

Purus
  • 11
  • 2
  • 4
    Possible duplicate of [How to access a variable from another script in another gameobject through GetComponent?](http://stackoverflow.com/questions/26551575/how-to-access-a-variable-from-another-script-in-another-gameobject-through-getco). You need to GetComponent with `GameController` then access the `tempPosition` variable from it. `GameObject.Find("GameControllerObJ").GetComponent().thePlatformTracker`. Please search before posting next time. – Programmer Nov 09 '16 at 16:44
  • May be attach in Platform Tracker component to field thePlatformTracker in GameController component? In inspector window – OnionFan Nov 09 '16 at 23:13

1 Answers1

0

To access thePlatformTracker correctly, you need to use GetComponent. In GameController.cs Start() simply change:

thePlatformTracker = FindObjectOfType<PlatformTracker>();

To:

thePlatformTracker = GameObject.Find("").GetComponent<Platform Tracker>();

Be sure and add the game controller object name between the GameObject.Find("") quotation marks.

Then you should be able to access the platformTransform no problem since thePlatformTracker is now correctly referencing the script.

KenSchnetz
  • 206
  • 2
  • 12