c#


Simplest way to target GameObjects properties when it triggers/collides?


If I have a trigger or collision I want to target that gameObject and have access to it's properties for each of the attached scripts. I managed to do it like this but I know there has to be a better method right?
Here I wanted to target the script attached to the triggered game object labeled "BoxManager" and then BoxManager's property "isHeld" If the target is being selected dynamically like this when it interacts with the trigger/collision I have to use the target object, but is there a better way to do the rest?
void OnTriggerStay2D(Collider2D target)
{
if (target.gameObject.tag == "Box")
{
if (target.attachedRigidbody.GetComponent<BoxManager>().isHeld == false)
{
target.attachedRigidbody.AddForce(transform.right * thrust);
}
}
}
First of all, target.attachedRigidbody.GetComponent<BoxManager>() is totally unnecessary and redundant. I've seen people even do worse with code such as target.attachedRigidbody.gameObject.transform.GetComponent<BoxManager>();. That's doing the-same thing because they are all attached to one GameObject.
You can and should simply that to target.GetComponent<BoxManager>();.
As for using GetComponent function all the time to get another script during collision, that's fine. It is the easiest way to get another Script that is attached to the GameObject during collision before you can access the variables in side that script.
Although, I find it annoying that you must do GetComponent in the OnTriggerStay2D just for that. It get's more annoying when you have so many scripts to check.
This is your collision function:
void OnTriggerStay2D(Collider2D target){}
And you need to access your BoxManager script from target after collision....
A tricky way would be to use a Dictionary. In the Start function initialize the Dictionary and use Collider2D as the key and BoxManager as the value.
Update this Dictionary when you instantiate and destroy GameObjects with Collider2D and BoxManager during game-play.
Something like this:
Dictionary<Collider2D, BoxManager> scriptID = new Dictionary<Collider2D, BoxManager>();
Then inside your collision function, use TryGetValue and the target variable as the key to get the BoxManager component.
void OnTriggerStay2D(Collider2D target)
{
if (target.CompareTag("Box"))
{
BoxManager result;
scriptID.TryGetValue(target, out result);
if (!result.isHeld)
{
}
}
}
In fact, if you %100 sure that this object has the BoxManager script attached to it, you can do that in one line of code with without the TryGetValue function:
void OnTriggerStay2D(Collider2D target)
{
if (target.CompareTag("Box"))
{
BoxManager result = scriptID[target];
}
}
You can reduce the chances of detecting Object without the BoxManager script by using target.CompareTag like above. Notice how I changed your target.gameObject.tag to target.CompareTag. It recommended to use target.CompareTag.
Now the code has been simplified with BoxManager result = scriptID[target]; while using target.CompareTag("Box") as a guard so that you don't have to use TryGetValue or the ContainsKey function.
Below is what the example of the whole script should look like:
Dictionary<Collider2D, BoxManager> scriptID = new Dictionary<Collider2D, BoxManager>();
public BoxManager boxManagerPrefab;
void Start()
{
//Register your BoxManager instances to the Dictionary
for (int i = 0; i < 5; i++)
{
BoxManager bc = Instantiate(boxManagerPrefab) as BoxManager;
//Use Isntance ID of the Script as id
Collider2D cld2D = boxManagerPrefab.GetComponent<Collider2D>();
scriptID.Add(cld2D, bc);
}
}
void OnTriggerStay2D(Collider2D target)
{
if (target.CompareTag("Box"))
{
BoxManager result = scriptID[target];
if (!result.isHeld)
{
target.attachedRigidbody.AddForce(transform.right * thrust);
}
}
}

Related Links

Error creating a line graph on winform
Is there a way to uniquely identify the website (or application) hosted in IIS?
Windows Service not functioning
Does HttpResponseStream buffers the data that it streams?
Audience URI validation failed. Audience does not match
How can i compare arrays in two jagged arrays C#
Implement RemoveRange in Entity Framework
How to Configure SSL in Quickfix/N
Getting error when trying to scaffhold APIController with foreignKey
Webservice API Nomadesk
String was not recognized as a valid Boolean Exception [closed]
How can i Put Linq result to multiple Array?
Not loading images at loading custom style from assembly
iOS/Unity plugin for QuickLook is crashing with (null)
HTTP Error 502.5 - Process Failure when application is run from Visual Studio
Try-catch doesn't wait for async function inside

Categories

HOME
algolia
xamarin.forms
x86-64
data-modeling
okhttp3
overrides
sdk
flowtype
procmon
open-search-server
markdown
doctrine2
samsung-gear-s2
dynamics-crm-2011
osclass
repeater
bookmarklet
owncloud
filehelpers
aiml
project-structure
spring-webflow
jq
procedural-generation
jira-zephyr
rollupjs
angular-schema-form
delphi-2010
ibm-bpm
ponylang
device-driver
const
iterm2
stdin
instances
pyexcel
api-key
ocamlbuild
opos
visio-2010
jcifs
bioperl
intermediate-language
webmatrix
xamarin-test-cloud
rmq
disassembly
zsh-completion
distcc
symantec
networkstream
particle-system
hotkeys
alchemy
ooad
ewam
biginsights
callfire
vensim
p4python
vectordrawable
data-representation
accpac
famous-engine
vundle
ios-enterprise
nvcc
.net-reflector
autofilter
node-amqp
mysql-error-1146
complete
ia-32
wpd
database-restore
yii-components
volatility
github-archive
towerjs
datejs
scriptmanager
yii-cactiverecord
wdm
session-hijacking
simultaneous
table-valued-parameters
browser-state
mysql-logic
file-icons
software-update
tracd

Resources

Encrypt Message