Skip to main content
added 250 characters in body
Source Link
jhocking
  • 15.8k
  • 2
  • 45
  • 59

How exactly this is best handled depends on the exact use case. The two main use cases are:

  1. You want to access a shared background object that persists throughout the game.
  2. You want to access a specific object within the scene.

For situation 1, you could use one of the code patterns like Service Locator or singleton (justin's suggestion is basically a Unity flavored singleton). Alternatively you could use FindObjectOfType() to reference an existing object in the scene.

For situation 2, you can simply drag the object onto a variable in the Inspector. In other words, create a serialized variable at the top of the script (eg. public Example2 otherScript) and then that variable will appear in the Inspector so that you can drag in an object in the scene. Now you can call any public methods of that object.

(incidentally, as zee points out the AddComponent command does exactly what it says, it adds a new component, not give you access to an existing component. If you don't want a new component added, then use a different command like GetComponent)

How exactly this is best handled depends on the exact use case. The two main use cases are:

  1. You want to access a shared background object that persists throughout the game.
  2. You want to access a specific object within the scene.

For situation 1, you could use one of the code patterns like Service Locator or singleton (justin's suggestion is basically a Unity flavored singleton). Alternatively you could use FindObjectOfType() to reference an existing object in the scene.

For situation 2, you can simply drag the object onto a variable in the Inspector. In other words, create a serialized variable at the top of the script (eg. public Example2 otherScript) and then that variable will appear in the Inspector so that you can drag in an object in the scene. Now you can call any public methods of that object.

How exactly this is best handled depends on the exact use case. The two main use cases are:

  1. You want to access a shared background object that persists throughout the game.
  2. You want to access a specific object within the scene.

For situation 1, you could use one of the code patterns like Service Locator or singleton (justin's suggestion is basically a Unity flavored singleton). Alternatively you could use FindObjectOfType() to reference an existing object in the scene.

For situation 2, you can simply drag the object onto a variable in the Inspector. In other words, create a serialized variable at the top of the script (eg. public Example2 otherScript) and then that variable will appear in the Inspector so that you can drag in an object in the scene. Now you can call any public methods of that object.

(incidentally, as zee points out the AddComponent command does exactly what it says, it adds a new component, not give you access to an existing component. If you don't want a new component added, then use a different command like GetComponent)

deleted 2 characters in body
Source Link
jhocking
  • 15.8k
  • 2
  • 45
  • 59

How exactly this is best handled depends on the exact use case. The two main use cases are:

  1. You want to access a shared background object that persists throughout the game.
  2. You want to access a specific object within the scene.

For situation 1, you could use one of the code patterns like Service Locator or singleton (justin's suggestion is basically a Unity flavored singleton). Alternatively you could use FindObjectsOfType()FindObjectOfType() to reference an existing object in the scene.

For situation 2, you can simply drag the object onto a variable in the Inspector. In other words, create a serialized variable at the top of the script (eg. public Example2 otherScript) and then that variable will appear in the Inspector so that you can drag in an object in the scene. Now you can call any public methods of that object.

How exactly this is best handled depends on the exact use case. The two main use cases are:

  1. You want to access a shared background object that persists throughout the game.
  2. You want to access a specific object within the scene.

For situation 1, you could use one of the code patterns like Service Locator or singleton (justin's suggestion is basically a Unity flavored singleton). Alternatively you could use FindObjectsOfType() to reference an existing object in the scene.

For situation 2, you can simply drag the object onto a variable in the Inspector. In other words, create a serialized variable at the top of the script (eg. public Example2 otherScript) and then that variable will appear in the Inspector so that you can drag in an object in the scene. Now you can call any public methods of that object.

How exactly this is best handled depends on the exact use case. The two main use cases are:

  1. You want to access a shared background object that persists throughout the game.
  2. You want to access a specific object within the scene.

For situation 1, you could use one of the code patterns like Service Locator or singleton (justin's suggestion is basically a Unity flavored singleton). Alternatively you could use FindObjectOfType() to reference an existing object in the scene.

For situation 2, you can simply drag the object onto a variable in the Inspector. In other words, create a serialized variable at the top of the script (eg. public Example2 otherScript) and then that variable will appear in the Inspector so that you can drag in an object in the scene. Now you can call any public methods of that object.

Source Link
jhocking
  • 15.8k
  • 2
  • 45
  • 59

How exactly this is best handled depends on the exact use case. The two main use cases are:

  1. You want to access a shared background object that persists throughout the game.
  2. You want to access a specific object within the scene.

For situation 1, you could use one of the code patterns like Service Locator or singleton (justin's suggestion is basically a Unity flavored singleton). Alternatively you could use FindObjectsOfType() to reference an existing object in the scene.

For situation 2, you can simply drag the object onto a variable in the Inspector. In other words, create a serialized variable at the top of the script (eg. public Example2 otherScript) and then that variable will appear in the Inspector so that you can drag in an object in the scene. Now you can call any public methods of that object.