1

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 ?

4
  • Well you shouldn't be treating MonoBehaviour-derived classes as regular classes and trying to instantiate them via new. Instead you add the scripts as components and add them to a Unity object in the editor. Besides, Start() is private Commented Aug 8, 2015 at 6:12
  • I added script as components. how can i access start() from another component's script ?? Commented Aug 8, 2015 at 6:15
  • You can't. 1) it's private 2) It's called only once by Unity 3) You shouldn't make assumptions on call order Commented Aug 8, 2015 at 6:19
  • Make a bolt prefab and assign it to PlayerControl Commented Aug 8, 2015 at 7:28

3 Answers 3

1

it's possible to call a Start() method from outside. just make it public.

public class MoverBolt : MonoBehaviour {
    public void Start () 
    {
        Debug.Log("MoverBolt.Start() was called");
    }
}

public class PlayerControl : MonoBehaviour {
    [SerializeField]
    private MoverBolt _moverBolt;

    void Start () 
    {
        _moverBolt.Start();
    }
}

The output in the console of this is

MoverBolt.Start() was called
MoverBolt.Start() was called

UPDATE 1

I would not recommend this, because the Start() method is called by your code and the game engine again.

When I need to make sure a MonoBehaviour is properly setup, before another class uses it. I replace the Awake/Start method with public void Initialize() method and call that from outside.

Sign up to request clarification or add additional context in comments.

1 Comment

I thought it was a bad programming practice to make start() public
0

Very simple answer. You can't Access start () function from any other scripts.

Comments

0

Use "Instantiate". For example, you can create of prefab of the game object that you want to make a copy of and then use the prefab to generate the new objects.

public class ObjectFactory : MonoBehaviour()
{
   public GameObject prefab;  // Set this through the editor.

   public void GenerateObject()
   {
      // This will create a copy of the "prefab" object and its Start method will get called:
      Instantiate(prefab);
   }
}

3 Comments

I made the prefab of the bolt but as soon as i enable the space ship the compiler shows error .If there is a start() in MoverBolt how can the PlayerControl can access rigidbody object (rb) so that all functions under it works ie ( rb.velocity,rb.position ect )
Put the initialization code in Awake instead of Start.
It worked but when i enter play mode at first i can move the Spaceship but after dragging and dropping bolt prefab into the editor the Spaceship freezes :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.