- Home /
Assigning value stored in an array with get{} using cSharp
Using cSharp, I am currently assigning the public float value shootForce as such :
public class Bullet {
public struct bulletData
{
private float _shootForce;
public float shootForce // The force at which missiles travel
{
get
{
_shootForce = 2f;
return _shootForce;
}
set{_shootForce = value; }
}
}
I'm accessing the value from other classes / scripts such as moveMissiles.cs in this way :
Bullet B = new Bullet();
void FixedUpdate ()
{
rigidbody.velocity = transform.up * B.playerBullet.shootForce;
}
How do I assign the value of shootForce to a value contained within an array defined outside the shootForce property.
Just out of curiosity, what is the advantage of using properties like this?
I see none at all.
(I have a feeling this question demonstrates why I prefer not to use IDEs)
In this context IDE means Visual Studio or $$anonymous$$onoDevelop.
I am curious if the only reason you're using properties is because your IDE encourages it.
Yeh, I'm rewriting the code. I have gotten rid of the get{} and set{} from shootForce. Once I have it fully working I'll put up an answer, though of course I may shuffle code around after.
If you wrote another question, why did you screw this one up? You edited the question 18 times!!! You totally changed the question and removed the titel. How should that be useful for others when you have answers that were correct for your 4th revision but you just exchanged everything? The edit button is there to improve the question, not to hide the actual question.
If you have another question, ask a new one. If this one is still not answered try to extend the question, but don't change it to something else. I've read comments about properties but there are no properties in your question (at least the last revisions).
That's actually the worst you could do to this site.
Answer by jahroy · Dec 17, 2011 at 11:22 PM
You would do it just like you would with any other value:
arrayOfFloats[someNumber] = B.playerBullet.shootForce;
The above line of code demonstrates how you would assign the value of some bullet's shootForce to element someNumber of an array named someArray.
If you wanted to set it to the first element of an array named funkyArray you would do it like this:
funkyArray[0] = B.playerBullet.shootForce;
Note that arrays start with element number zero for most programming languages.
Your answer
Follow this Question
Related Questions
Store reference to array as variable 2 Answers
How to check if vector in array was not changed? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers