- Home /
C# Get Component Question
I am quite confused as to how GetComponent works and I had a question about how to access a certain function of a script or a variable.
I have a c# script that when i press one, it will access a cannon, which has a bullet attached to it, and i want to access the bullets script.
so something like:
Cannon -> Bullet -> Bullet's Script -> variable x = 10 OR void xIsTen() gets activated
Answer by robhuhn · Nov 28, 2011 at 10:20 AM
If you instantiate the bullet prefab, you will have something like
GameObject bullet = GameObject.Instantiate(bulletPrefab, ...);
The bullet will have the script "CannonOneBullet" attached. So you can write
CannonOneBullet cannonOneBullet = bullet.GetComponent<CannonOneBullet>();
cannonOneBullet.fSpeed = 20.0f;
Thank you so much, 1 last tiny problem though, I have the GridSelection script that does all of the selection for the cannons, then I have the Cannon script where I have the bullet getting instantiated, so I can't really do what you wrote up there unless with the GridSelection script I have a send$$anonymous$$essage to Cannon script and then the above will work.
So if I don't have the GameObject bullet in the GridSelection script, can I still access it?
I don't understand exactly what you mean. You definitely need the gameObject. $$anonymous$$ay be you can store the current bullet instance in a variable to access it like "cannon.currentBullet.GetComponent". On the other hand GameObject.Find or GameObject.FindGameObjectsWithTag could help you, but I always try to avoid these methods if I can. You also can call GetComponentInChildren() in GridSelection if all gameObjects are parented. To parent the bullet write "bullet.transform.parent = this;" That will let the cannon be the parent of the bullet.
Answer by ChefZweegie · Nov 28, 2011 at 02:49 AM
In C#, you will need to cast the component over, since GetComponent returns a different data type. All you need to do is cast it over. In my example below, I have all of the movement for a multiplayer game stored in a different class (called PlayerInfo). I create a default movement class for all players, and then pull in the component. So, if I had two scripts, one named PlayerInfo.cs and another named Movement.cs, I could add the following code in Movement.cs to use functions and variables from PlayerInfo.cs
PlayerInfo playerInfo;
playerInfo = GetComponent(typeof(PlayerInfo)) as PlayerInfo;
I appreciate the help however, I don't know how to access it properly.
I have GridSelectionNew and CannonOneBullet scripts.
In my GridSelectionNew script I have:
private CannonOneBullet CanOneBul;
void Start() { CanOneBul = GetComponent(typeof(CannonOneBullet)) as CannoneOneBullet }
void Update() { if(condition) { CanOneBul.fSpeed = 20.0f; } }
In my CannonOneBullet script I have:
public fSpeed = 10.0f; //was private but had to change it to public because i got an error saying CannonOneBullet.fSpeed is inaccessible due to its protection level.
Anyways, whenever the condition is met, I get an error saying: NullReferenceException: Object reference not set to an instance of an object.
I should have mentioned that the cannon has a public gameobject variable where I dragged the bullet into it and then whenever the cannon fires, it creates an instance of that gameobject as a rigid body. So I am guessing I have some sort of problem with accessing it because the instance isn't alive at all times or that I am trying to access it all wrong in general.
Answer by Ppa0 · Dec 01, 2011 at 09:32 PM
thank you very much
Did you get things to work? If so, post up the solution:) Lemme know if things are still a bit out of order!
Right now I am in the point of my game where I am just implementing basic things here and there and I decided that I will not really go into that right there until I am done with the game, if at all, so I did not end up using it. As far as everything else goes, Send$$anonymous$$essage is so much simpler and I know how to implement that so for right now that is what I am sticking with whenever I need to access something. $$anonymous$$aybe Later down the road when I know how to program better I might go back and try to optimize/use GetComponent ins$$anonymous$$d of Send$$anonymous$$essage. I do appreciate the help though thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613055400im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get the child gameObject of a parent and not the Transform 4 Answers
enable and disable boxcollider (whats wrong with my script?) 2 Answers
Obtaining an array of positions from an array of gameobjects 2 Answers
Only change a variable on the instaniated object not the prefab. 0 Answers
fix script to get C# var from another object instead of object this script is attached to 2 Answers