Question by
Torhque · Mar 09, 2018 at 04:20 AM ·
scripting probleminstantiateinstantiate prefabinstantiationassign-variable
Assign variables on Instantiation?
Goal: I need my bullet prefab to ignore collision with its shooter's shield. Note: I cannot ignore all shields. My Player prefab provides each player with a shield:
// SHOOTER SCRIPT
Start() {
shooterShield = GetComponentInChildren<Shield>(); // Find the shooter's shield. Check!
}
Shoot() {
obj_theBullet = (GameObject)Instantiate (bulletPrefab, ...);
// Get access to [obj_theBullet]'s Bullet component
bullet = obj_theBullet.GetComponent<Bullet>();
bullet.shield = shooterShield; // [Bullet]'s shield var is set to [Shield]'s shooterShield
bullet.shooterTf = this.transform; // Using this for comparison later
}
Then my Bullet script does:
public Shield shield; // Shield is assigned just fine from earlier
public Transform shooterTf; // shooterTf is assigned from earlier as well
void Start () {
// Ignore collision between the [Shield] and [this] bullet
Physics.IgnoreCollision( shield.GetComponent<Collider>(), this.GetComponent<Collider>() );
}
void OnCollisionEnter(Collision hitInfo) {
if (hitInfo.transform == shooterTf.transform)
// Do nothing.
}
Results: Using Debug logs, all this works flawlessly for the Host. However, the Bullet script's shooterTf variable is null for the joining player. How do I fix this?
Comment
Your answer
Follow this Question
Related Questions
Change value of a static variable multiple times in one script 2 Answers
Infinite Instantiation 2 Answers
How to Change the Variables of an Instantiated Object? 0 Answers
Add Component with Script Data 1 Answer
How to change the Bool for only a single prefab GameObject creating with Instantiate? 2 Answers