hi. I am currently making a 2d space shooter and i have got stuck on a part i know what needs to be done but i cannot grasp how to do it .
So i instantiate my shield in the player script and in my shield script i have my shield strength so what i need to do is when my shield strength gets to 0 in my shield script i want a boolean in my player script to be false and the variable is called shieldOn.
thanks in advance
Posting you code might help. There are several ways to do this (and thus solve your issue).
Are both scripts (player and shield) components of the same game object?
If there are two game objects, is the shield object child of the player game object?
Sorry, I’ve just read the post caption. So there are two game objects. Are they nested?
nested i am sorry i dont understand this term!!! ok the shield gets made when the user presses E so yes it is a child of the player object and the shield script is attached to the shield object not the player
Are you using C# or UnityScript?
i am using unity script sorry should have mentioned that!!
and also how do i make it so when the shield is on you cannot make another shield until it gets destroyed !!
thanks for the help in advance
I have seen this question appear frequently, I too struggled with the exact syntax at first. I think the Reference needs some work there.
Following code snippets should give you basic idea about how this could be achieved in C#. I don’t do Unity script. I hope you’ll be able to translate C#.
You can’t just copy-paste the code. You need to add input and collision check. Also I might have made some wrong assumption(s) as you haven’t given any code.
Player.cs
public class Player : MonoBehaviour
{
public GameObject prefabShield;
private bool shieldOn = false;
void Update()
{
// check if shieldOn is false will prevent from instantiating multiple shield objects
if (!shieldOn E-pressed)
{
GameObject goShield = (GameObject)Instantiate(prefabShield);
goShield.transform.parent = transform;
goShield.transform.position = Vector3.zero;
Shield shield = goShield.GetComponentInChildren<Shield>();
shield.shieldChanged += new ShieldValueChanged(Shield_shieldChanged);
shieldOn = true;
}
}
private void Shield_shieldChanged(GameObject goShield, float val)
{
if (val <= 0.0f)
{
shieldOn = false;
// destroy goShield
}
}
}
somewhere:
public delegate void ShieldValueChanged(GameObject goShield, float val);
Shield.cs
public class Shield : MonoBehaviour
{
public event ShieldValueChanged shieldChanged;
private float val;
void Start()
{
val = 100.0f;
}
void Update()
{
if ((val > 0.0f) collision-happens)
OnShieldChanged(val);
}
private void OnShieldChanged(float val)
{
if (shieldChanged != null)
shieldChanged(gameObject, val);
}
}
Feel free to ask if anything is unclear.
EDIT: I modified the code a bit so you’re able to destroy shield object from Player script.
ok here is code off what i have so far !
script player:
var numberOfShields : int = 4;
var shieldMesh : Transform;
var shieldKeyInput : KeyCode;
function Update ()
{
// create a shield
if(Input.GetKeyDown(shieldKeyInput))
{
if(!shieldOn)
{
var clone = Instantiate(shieldMesh, transform.position, transform.rotation);
clone.transform.parent = gameObject.transform;
shieldOn = false;
numberOfShields -=1;
}
}
if(numberOfShields <= 0)
{
numberOfShields = 0;
}
}
}
script Shield :
var shieldStrenght : int = 3;
function Update ()
{
if(shieldStrenght <=0)
{
Destroy(gameObject);
}
}
script astroid:
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "shield")
{
other.GetComponent("script shield").shieldStrenght -=1;
if(explosion)
{
Instantiate(explosion, transform.position, transform.rotation);
}
// Reset the position of the astroid when it collides with the player
ResetEnemy(); // This is a function that i craeted
}
}
so the astroid script is working fine ! that is the collision part
what i need to know is how to make the shield only be used 4 times and that when a shield has been mad you cannot make another one until that shield is destroyed !
thanks in advance and please post if any other info is needed
Please, edit your post and put the code segments between
tags.
You can click Go Advanced and preview the post until it looks nice.
ok done any help ?
@ Nikolic sorry but i do not understand c sharp but i am planning on learning after unity script !!
Bump help would be appreciated !
Bumb please help I have been trying to solve this issue for 3 days now I can’t get to the bottom of it !
Thanks for that info I have been trying to make it work with that info and I have looked at many other ways but I just can’t find the solution!!
Please help !!!
It looks like what is happening is that in ‘script astroid’ you try to access ‘script shield’ when object ‘other’ tagged ‘shield’ enters the trigger. The reason why the code is not working(you are getting an error) is that each script does not define a class, hence in code you cannot reference that script as a value. When you try to get a component using a string(the name of the script) GetComponent returns the type Component. Since your script doesn’t define a class you cannot manually convert the returned type to your script type, therefore you need to use dynamic typing to have the computer convert it for you. This requires you to remove ‘#pragma strict’ from your script code, since this article shows that you should use ‘#pragma strict’ then you should define a class with your scripts. You could do that like this:
//note that class names cannot contain spaces and it needs to be exactly the same as the name of the script(file)
public class ScriptName extends MonoBehaviour {
//The script code that you already have.
//don't forget to place 'public' in front of the variables though
}
Then you would access it like this:
someGameObject.GetComponent(ScriptName).someValue = someOtherValue;
This way you should not get any errors.
~Atin
Hi thanks very much for that but I cannot read c sharp do you think you will be able to translate into unity script
Thanks 
UnityScript files will automatically extend MonoBehaviour unless you specify otherwise so this is not accurate.
However - this is accurate. You should never use the string overload of GetComponent. It’s slower and only returns a Component object that you would have to cast explicitly anyway.
Also - I wouldn’t put spaces in a script name. I haven’t seen anything that says “it doesn’t work” but I’ve heard of folks running into issues because of this. The editor formats everything nicely for you anyway so long as you use Pascal or Camel Case so it becomes even more unnecessary.
Could someone please translate Atin skritas example into unity script please