Question by
JerryLetehen · Jul 26, 2017 at 05:50 PM ·
unity 5constructor
How to use constructors (addComponent)?
Hello! I have two scenes. In first I a have 3 parameters. This parameters must change other 3 parameters in 3 classes I have(2nd scene). I thought I need a constructor, but I can't do this. Now I know that I should use AddComponent, but I don't know how to pass parameters through it. Need help!
public class SplashScene : MonoBehaviour {
public string sceneToLoad;
public int BallSpeed = 30;
public int PaddleSpeed = 30;
public int AISpeed = 30;
public void Start_it()
{
Debug.Log(PaddleSpeed + " " + AISpeed + " " + BallSpeed);
OpenNextScene(PaddleSpeed, AISpeed, BallSpeed);
}
void OpenNextScene(int left, int right, int ball) // left = LeftPaddleSpeed, right = AIPaddleSpeed, ball = BallSpeed
{
// change next lines
MovePaddle leftPaddle = new MovePaddle(left);
AIPaddle rightPaddle = new AIPaddle(right);
Ball ballSpeed = new Ball(ball);
SceneManager.LoadScene(sceneToLoad);
}
}
And classes of 2nd scene:
public class MovePaddle : MonoBehaviour {
private int speed = 30;
public MovePaddle(int speed)
{
this.speed = speed;
}
}
public class Ball : MonoBehaviour {
private int speed = 30;
public Ball(int speed)
{
this.speed = speed;
}
}
public class AIPaddle : MonoBehaviour {
private int speed = 30;
public AIPaddle(int speed)
{
this.speed = speed;
}
}
Comment