How can i access start() function from another script since start function can be only defined once
This is the script containing start() -
using UnityEngine;
using System.Collections;
public class MoverBolt : MonoBehaviour {
public PlayerControl obj ;
public float speed ;
public Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody>();
rb.velocity = transform.forward * speed;
}
}
Script which need to access start()
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary{
public float xMax,xMin,zMax,zMin;
}
public class PlayerControl : MonoBehaviour
{
public Boundary boundary ;
public float velocity;
public float tilt;
MoverBolt obj = new MoverBolt();
/* I made an object but it seems you are not supposed to create an object of class which is inheritance of MonoBehaviour */
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
obj.rb.velocity = movement*velocity;
Vector3 current_position = obj.rb.position;
obj.rb.position = new Vector3 ( Mathf.Clamp(current_position.x,boundary.xMin,boundary.xMax),
0.0f,
Mathf.Clamp(current_position.z, boundary.zMin, boundary.zMax)
);
obj.rb.rotation= Quaternion.Euler(0.0f,0.0f,obj.rb.velocity.x*-tilt );
}
}
Error You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent().
Are there any alternatives ?
MonoBehaviour-derived classes as regular classes and trying to instantiate them vianew. Instead you add the scripts as components and add them to a Unity object in the editor. Besides,Start()is private