- Home /
Instantiate Object as GameObject
So, I want to instantiate an Object and then set a value on a component on this same object.
This is the code of the spawner object(a gun).
GameObject firedbolt = Instantiate (bolt, Muzzle.position, Muzzle.rotation) as GameObject;
firedbolt.GetComponent<ProjectileController>().Owner = Owner;
I'm trying to make this to prevent the bolts fired by an object, hit its controller. As in, Player A can hit Player B, or Player C, or any NPC, but can't hit itself, another bullet from the same controller, or the controller itself.
The problem is, I'm getting "NullReferenceException:Object reference not set to an instance of an object" in the those two line and the "Owner" variable is not been set, and I don't know why. as all variables in the spawner are set.
You need to know which one is the source of error first. Debug log the firedbolt, firedbolt.GetComponent(), and Owner
. Check which of these give the null reference error.
After changing a bit the code to this:
var firedbolt = Instantiate (bolt, $$anonymous$$uzzle.position, $$anonymous$$uzzle.rotation) as GameObject;
firedbolt.gameObject.GetComponent<ProjectileController>().Owner = Owner;
now only this line returns the NullReference error:
firedbolt.gameObject.GetComponent().Owner = Owner;
Apperently the firebolt is not set.
Answer by ChrPunx · Feb 06, 2015 at 09:40 PM
ok...managed to "fix" it, everytime my "gun" is going to shot a new bolt, it set it's owner.
bolt.Owner = Owner;
Instantiate(bolt, Muzzle.position, Muzzle.rotation);
Answer by alex31016 · Feb 06, 2015 at 04:08 AM
Have you tried something like:
Physics.IgnoreCollision(bullet.collider, transform.root.collider);
yes, I already use that on my projectile controller script. Not just ignore collision with the "owner" it desables collider if there is no "owner" defined.
the problem is that the "owner" variable of the weapon is not been set on the instantiated bolt
Answer by alok-kr-029 · Feb 06, 2015 at 04:36 AM
is your bots getting Instantiated if no than the script is not getting the prefab if yes than bots are not getting the projectile controller script
For collision detection assign layers to the game objects and use collision matrix table Hope it will work
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
change a gameobject into another 1 Answer
Get game component type? 1 Answer
Issue with GameObject has been destroyed but it is not destroyed 1 Answer