0
\$\begingroup\$

How to add script based on current scene? I try

GameObject car = GameObject.FindGameObjectWithTag("InGame");

if (SceneManager.GetActiveScene().name == "TestDrive")
        {
            car.AddComponent<CarCtrl>();
        }

but nothing what I expected I want if I am on scene TestDrive there it attaches script CarCtrl to car
I am using also DontDestroyOnLoad (because I want to pass 3d model between two scenes CarSelect and TestDrive) but have issue because the script attach to previous scene also (this is CarSelect) but I want only to attach the script in scene TestDrive
I have the answer I should use level manager but I can't find way to start

CarCtrl in TestDrive scene

using System.Collections.Generic;
using TMPro;
using UnityEngine;
using Rgn;

public class CarCtrl : MonoBehaviour
{
    private Rigidbody rb;
    private BoxCollider bc;

    public float horizontalInput;
    public float verticalInput;

    public float steerAngle;

    public List<Transform> Wheels;

    [Header("input")]
    public float steerInput;

    public float turnSpeed = 10f;

    public float maxSteerAngle = 30f;
    public float engineForce = 20;

    public const int topSpeed = 300;

    private TextMeshProUGUI speedText;

    private float speed = 0.0f;

    public void GetInput()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        verticalInput = Input.GetAxis("Vertical");
    }

    private void Steer()
    {
        var dt = Time.deltaTime;
        steerAngle = maxSteerAngle * horizontalInput * dt;
        var smoothedSteering = Mathf.Lerp(0.0f, steerAngle, 10);
        var turnForce = transform.up * turnSpeed * smoothedSteering;
       // Debug.Log(turnForce);


        Wheels[0].Rotate(transform.forward * -1, steerAngle);
        Wheels[1].Rotate(transform.forward * -1, steerAngle);

        rb.AddRelativeTorque(turnForce, ForceMode.Acceleration);
    }

    private void Accelerate()
    {
        // speed in m/s
        speed = rb.velocity.magnitude;
        var force = transform.forward * -1 * engineForce * verticalInput;
        var speedKph = Car.MpsToKmh(speed);
        if (speedKph < topSpeed)
        {
            var speedtxt = speedKph.ToString();
            rb.AddForce(force, ForceMode.Acceleration);
            speedText = (TextMeshProUGUI) FindObjectOfType<TextMeshProUGUI>();
            speedText.text = $"speed: {speedtxt} kmh";

            rollWheels();
        }
    }

    private void FixedUpdate()
    {
        GetInput();

        Accelerate();

        Steer();
    }



    // Start is called before the first frame update
    private void Start()
    {
        //stop rotating
        Destroy(GetComponent<Turn>());


      //  AddWheels();



        var car = new Car("make", "model");
        print(Car.AckermannLeft(2, 2, 10));



        this.gameObject.transform.Translate(-30, 0, 0, Space.World);

       rb = GetComponent<Rigidbody>();

        rb.useGravity = true;

    }

    private void rollWheels()
    {
        var angle = engineForce * verticalInput;
        foreach (var wheel in Wheels)
        {
            wheel.Rotate(Vector3.right, angle);
        }
    }



    private void AddWheels()
    {
        Transform fl = GameObject.Find("Wheel_FL").transform;


        Transform fr = GameObject.Find("Wheel_FR").transform;
        Transform rl = GameObject.Find("Wheel_RL").transform;
        Transform rr = GameObject.Find("Wheel_FL").transform;

        Wheels.Add(fl);
        Debug.Log(Wheels);

        Wheels.Add(fr);
        Wheels.Add(rl);
        Wheels.Add(rr);
    }





}

DontDestroyOnLoad

 using UnityEngine;

public class DontDestroyOnLoad : MonoBehaviour
{
    // Start is called before the first frame update
    private void Start()
    {
        GameObject car = GameObject.FindObjectOfType<Turn>().gameObject;



        var camera = GameObject.Find("Main Camera");

        camera.transform.parent = car.transform;

    }

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

Scenes

using UnityEngine;
using UnityEngine.SceneManagement;

public class Scenes : MonoBehaviour
{
    // Start is called before the first frame update
    private void Start()
    {
    }

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

    public void ChangeToMenu()
    {
        SceneManager.LoadScene("Menu");
    }

    public void ChangeToTracks()
    {
        SceneManager.LoadScene("Tracks");
    }

    public void ChangeToMakes()
    {
        SceneManager.LoadScene("Makes");
    }

    public void ChangeToModels()
    {
        SceneManager.LoadScene("Models");
    }

    public void ChangeToGarage()
    {
        SceneManager.LoadScene("Garage");
    }

    public void ChangeToCarSelect()
    {
        SceneManager.LoadScene("CarSelect");
    }

    public void ChangeToTestDrive()
    {
        SceneManager.LoadScene("TestDrive");
    }
}
\$\endgroup\$
0
0
\$\begingroup\$

Ok I know why this not working because I have attached script CarCtrl to prefab initially in inspector and I should disable script on CarSelect and enable on TestDrive and no issue with it but disable in DontDestroyOnLoad in CarSelect

var carCtrl = GameObject.FindGameObjectWithTag("InGame").GetComponent<CarCtrl>();
                carCtrl.enabled = false;

also make enabler of script in TestDrive

if (SceneManager.GetActiveScene().name == "TestDrive")
        {
            var carCtrl = GameObject.FindGameObjectWithTag("InGame").GetComponent<CarCtrl>();
            carCtrl.enabled = true;

        }
\$\endgroup\$
1
  • \$\begingroup\$ Don't forget to mark this answer as Accepted if it worked for you. \$\endgroup\$
    – DMGregory
    Nov 21 at 0:24

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.