0
\$\begingroup\$

If pointName and player.startPoint are true, shown by the inspector, why doesn't the player's position go to the start point with a pointName of "ToNeighborhood". Note: The code runs if pointName == player.startPoint. It isn't running even when they both contain the same string shown by the inspector

Image1 enter image description here enter image description here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStartPoint : MonoBehaviour {

    private PlayerController player;
    private CameraController theCamera;

    public string pointName;

    // Use this for initialization
    void Start () {
        player = FindObjectOfType<PlayerController>();
        theCamera = FindObjectOfType<CameraController>();
        if (pointName == player.startPoint)
        {
            Debug.Log("Changing");
            player.transform.position = gameObject.transform.position;
            theCamera.transform.position = new Vector3(transform.position.x, transform.position.y, theCamera.transform.position.z);
        }

     }

    // Update is called once per frame
    void Update () {

    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Is it possible that your if statement is wrong? You´re comparing the pointName to the object player.startPoint, but should really compare e.g. two point names, I think. Correct me if I´m wrong. \$\endgroup\$ – Ignatiamus Jan 9 '18 at 20:18
  • \$\begingroup\$ I believe that is wrong since the exit point (the collision) assigns its string to the string of the player, then it checks to see if the point name is equal to the player. So essentially it is basically checking if the two points are equal. \$\endgroup\$ – JFZhang Jan 10 '18 at 4:22
0
\$\begingroup\$

There is actually no reason to assign a variable on Start, if you can do so otherwise. i.e. from the inspector, as it's quite performance intensive.

For your purposes of doing this, you could simply make your player and camera controller public, and assign them via the editor.

By doing that your code could look like the following :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStartPoint : MonoBehaviour {
    public PlayerController player;
    public CameraController theCamera;

    public string pointName;

    // Use this for initialization
    void Start () {
        if (pointName == player.startPoint)
        {
            Debug.Log("Changing");
            player.transform.position = gameObject.transform.position;
            theCamera.transform.position = new Vector3(transform.position.x, transform.position.y, theCamera.transform.position.z);
        }

     }

}

Please note also that your UPDATE method has been removed. If it's empty, always remove it, as it's still called, and so takes time to run even if its to do nothing.

If you DO wish to assign your variables on Start, you can do the following :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStartPoint : MonoBehaviour {

    private PlayerController player;
    private CameraController theCamera;

    public string pointName;

    // Use this for initialization
    void Start () {
        player = GameObject.Find("NAMEOFPLAYER").getComponent<PlayerController>();
        theCamera = GameObject.Find("NAMEOFCAMERA").getComponent<CameraController>();
        if (pointName == player.startPoint)
        {
            Debug.Log("Changing");
            player.transform.position = gameObject.transform.position;
            theCamera.transform.position = new Vector3(transform.position.x, transform.position.y, theCamera.transform.position.z);
        }

     }

}
\$\endgroup\$
-1
\$\begingroup\$

You have to do like this..

player = Gameobject.Find("PlayerController").getComponent<ScriptName>();
theCamera = Gameobject.Find("CameraController").getComponent<ScriptName>();

hope it will work..

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.