Jump to content

  • Log In with Google      Sign In   
  • Create Account


Unity Javascript: Calling var from other script


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 afliii   Members   -  Reputation: 464

Like
0Likes
Like

Posted 01 February 2012 - 04:27 PM

This is probably very simple, but I'm new to Unity and Javascript in Unity.

I'm trying to do an ammo pickup. My game is 2D. I've programmed shooting and loss of ammo, but I need the ability to reload with a new clip. I thought the way to call a variable from another script was simple, like this: scriptNameHere.varNameHere but maybe I misunderstood something in a tutorial somewhere....

The character needs to collide (this already happens) with the ammo box. The weapon must refill that variable by 5 when the collision occurs. The reason I need to call this var from one script to another is because my character and his weapon are two separate objects, otherwise the projectile (which is already set up and functional) hits the character and never really goes anywhere.

I'd appreciate some advice and specific method of calling the var between scripts. I could probably ask a million more questions, but whatever, I'll figure that stuff out as I go.
Additionally, by team (Broken Limits) is looking for a programmer. As you can see, we need one. Learning on the job makes it kind of miserable and slow, but I'll be here to back up a programmer if we can attain one.

Thanks,
-Allen

Sponsor:

#2 reisama   Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 February 2012 - 06:40 PM

It's been a while since i used unity, but if i remember correctly all you need to do is have your player be aware of what weapon he is holding by storing it in a variable inside the player object.

Then you will have no problems accessing the ammo values of the weapon. You can also just have the player object hold the ammo information, since spare ammo is usually on the player and not on the gun, and have the weapon retrieve the ammo information from player when reloading the weapon.

#3 afliii   Members   -  Reputation: 464

Like
0Likes
Like

Posted 01 February 2012 - 07:23 PM

I'm kind of doing that.. and having trouble.

The weapon has the variable---- var ammoCount : int = 5;
the weapon, when fired, does this...


Shoot.js

function Update()
{
if( Input.GetButtonDown("Shoot"))
{
if ( ammoCount >=1 )
{
ammoCount -= 1;
}
}
}
this successfully subtracts my ammo, but I need to add ammo when my character collides with the ammo box. the above script is attached to an empty game object which is the child of the character..
the player script should do something like this...
playerController.js


function OnControllerColliderHit(hit : ControllerColliderHit)
{
if (hit.gameObject.CompareTag ("ammo"))
{
print ("hit ammo box"); //this tells us that the collision with ammo boxes works, and it does btw
//here I need help. there should be code here telling the weapon script to add 5 to the variable ammoCount
//i tried this..
Shoot.ammoCount += 5;
}
}

Please try to splain this in layman's terms.. I'm new to Unity.. and more particularly new to programming! Never programmed before, ever.. =(

Also, if someone is willing I could use help trying to implement sprite sheets. I don't really have a full blown sprite sheet, but I need to know how to use them in unity. Where should I look? Again.. if you want to actually do this for me contact me Please! Be a part of our team, Broken Limits.

#4 reisama   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 February 2012 - 12:23 PM

I don`t know much about javascript, however if you want I could write you the basic code in c#. I would need to reinstall unity, but if you can wait till the weekend I could provide you with a sample project.
Otherwise, here is my take on the problem at hand.

If i understand you currently have three in game objects: Gun object, Player object and ammo object(the one on the ground your player picks up)
I would create a script for each object.
Gun script would hold two functions, fire and addAmmo, plus a pick up function(optional depending on your game model)
fire function, does just that, fires your gun.
addAmmo function adds +5 to your ammo count.

Player script would hold a variable called MyCurrentGun pointing to the gun script that the player is currently using.
This script would also be responsible for checking for any user input, and when the user pressed the fire button, or key it would
call MyCurrentGun.fire() or however you do it in javascript

Ammo script would wait for a collision with a player.
When collision ocours, the ammo script would access the MyCurrentGun variable of the coliding object and call the addAmmo function.

Sorry i can`t help you more at the moment, i haven`t worked with javascript before.

#5 afliii   Members   -  Reputation: 464

Like
0Likes
Like

Posted 02 February 2012 - 03:08 PM

That's OK. Thanks for the efforts! You're understanding of the matter is correct, too. I'll keep playing around with it. I really think I'm just writing something wrong. I know exactly what needs to happen, but I can't figure out the correct code to even allow the game to run and test.

If anyone else has anything to add, please do. I need to get this out of the way and move on to other things. Thanks.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS