Getting float variable of child object in uinty - unity3d

I have Unit gameObject which contains 2 subOnjects in it: Monster and HealthBar.
Now Health has float - max_Health which I want to access using onTriggerEnter.
print(co.transform.GetChild(1));
Gives me "HealthBar" Which means I get correct subobject but now how can I get meaning of its float variable "max_Health" ?
I've tryied this:
print(co.transform.GetChild(1).transform.Find("max_Health"));
But got Null. That's wrong. How can I get it ?
print(co.transform.GetChild(1).Find("max_Health")); not working
print(co.transform.GetChild(1).Find("float max_Health")); not working.
What else can be done ??????
UPDATE
GameObject GG = GameObject.Find("GameObject co.transform.GetChild(1)");
Health bScript = GG.GetComponent<Health>();
print(bScript.max_Health);
gives me error
NullReferenceException: Object reference not set to an instance of an object
Tower.OnTriggerEnter (UnityEngine.Collider co) (at Assets/Scripts/Tower.cs:23)

Related

Null Reference annoying exception

(Issue is open on GitHub Here)
I am building a health bar for a fighting game in Unity, and for some reason the Health bar text does not appear and all it gives me is a null reference error, yet I've checked that everything it references is not 'null'.
The code for the text is here:
function Update () {
canvasText.text = brawlers[0].currentHealth + "/" + brawlers[0].myClass.health;
canvasText2.text = brawlers[1].currentHealth + "/" + brawlers[1].myClass.health;
}
The Brawlers array refers to two classes, each of them like this:
#pragma strict
var className : String;
var health : int;
var Attack : int;
var Defense : int;
var Speed : int;
I created a prefab with only the class attached and gave the health a value of 120. This prefab is attached to the fighters in the game, which are the brawlers array, as the 'myClass' varible. CurrentHealth is a varible initilized in the Brawler class.
I have tried multiple types to trace the parts that wouldn be null in the project but I have not found anything that could mean that this error is thrown.
Why is there a null reference exception then?
Your log clearly shows that their is a null reference exception in Start of Battle.cs script at line 15. Debug the issue by checking how it is assigned if assigned and is there some way that it is destroyed or unassigned.

How to move an object up with an action?

Wasn't exactly sure how to title this, but I need to count up how many enemies a player killed before they die and respawn for my point and health system.
My idea is to set up a variable that gets 1 added to it on each collide and then when the player is killed it takes the amount of that stored score variable and uses it for the point and for my health system, which is to move something up on the screen up.
Some super pseudo code would look like:
var storedPlayerScore = 0
var HealthPlus = SKAction.moveBy(CGVectorMake(0, (10 * storedPlayerScore)), duration: 0.05)
I've got both of those defined in my Gamescene.
and then down in my didbegincontact I'd just do like storedplayerscore++ whenever the two correct objects collide.
So before when I was just making sure the health going up worked, I had a number in where the stored variable was, but when I use this it gives me "Gamescene.type doesn't have a variable named storedPlayerScore"
Am I on the right track here? How do I clear the error I'm getting?
You are trying to access something on the type itself rather than on an instance of that type.
class GameScene {
var storedPlayerScore: Int = 0
}
var score1 = GameScene.storedPlayerScore // Error
let myGameScene = GameScene()
var score2 = myGameScene.storedPlayerScore // Works
This confusion is probably brought on by the names you're choosing for your variables. In your code above, you have a variable called HealthPlus which is named with a capital first letter. That makes it look like a type instead of an instance. The convention for variable names is start with a lowercase letter, while type declarations start with a capital letter. So it should be healthPlus instead.

How to call a function of scriptB from scriptA

I have created two scripts one is scrpt_Enemy and other is scrpt_GameManager.
I want to call SubtractLive() function (exist on scrpt_GameManager) from scrpt_Enemy. But it gives error I don't know why. The error is 'SubtractLive' is not a member of 'UnityEngine.Component'.
scrpt_Enemy:
var gameManager : GameObject;
gameManager.GetComponent("scrpt_GameManager").SubtractLive();
scrpt_GameManager:
var lives : int =3;
function SubtractLive(){
lives -= lives;
}
Problem
You were telling Unity that gameManager was of type GameObject. Therefore Unity was freaking out because it doesn't have SubtractLive() under GameObject.
Solution
You can get the correct reference like this:
Create the variable then tell Unity what the type it is.
var gameManager : scrpt_GameManager;
Find the GameObject which has the script attach to it.
gameManager = GameObject.Find("GameManagerGameObject").GetComponent("scrpt_GameManager");
Note that "GameManagerGameObject" is the name of the GameManager in the scene.
Now that you have the reference to the script just call the function like this:
gameManager.SubtractLive();

Walker boys tutorial... NullPointerException error

I am seeing the Walker boys tutorial, Im on the Project #2, where you make a ship that destroys asteroids... and there's a part where the bullet must call a function from other object... (here's the video http://vimeo.com/19463502) I do everything that is here but I get this error:
"NullReferenceException: Object reference not set to an instance of an object
scriptBullet.OnTriggerEnter (UnityEngine.Collider other) (at Assets/scriptBullet.js:39)
and that line of code is :
sceneManager.transform.GetComponent(scriptsceneManager).AddScore();
scriptsceneManager.js
var gameTime : float = 60;
static var score : int = 0;
function Update ()
{
print("score : " +score);
}
public function AddScore()
{
score +=1;
}
From what i can see (it's been a while since i did the walkerboy tutorials) there are only 2 reason why you can get a NullPointerException here.
The SceneManager has not been initialised in scriptBullet.js. If you are finding the GameObject by Tag make sure you have the tag assigned and is spelled correctly in the script. I always forget to assign tags until it's no late.
The scriptsceneManager hasn't been added to the SceneManager object. This results in the GetComponent call returning null.
If you are using Monodevelop, remember if you hit the attach button, you can attach UnityEditor to monodevelop, then you can use breakpoints to find out where the NullPointer is coming from.
https://docs.unity3d.com/Documentation/Manual/Debugger.html
Hope this helps.

Trying to access variables from another script in unity (Not Homework)

I am currently playing around in Unity trying to make/test a 2D game. I keep getting the following error when I attempt to access CharacterMotor.playerx from inside camerafollow.js:
An instance of type "CharacterMotor" is required to access non static member "playerx"
Here are my two scripts:
camerafollow.js
#pragma strict
function Start () {
transform.position.x = CharacterMotor.playerx;
}
CharacterMotor.js
#pragma strict
#pragma implicit
#pragma downcast
public var playerx : float = transform.position.x;
You could change playerx to static, but I don't think that's what you want to do (there's probably only one player object, but this would prevent you from ever having multiple CharacterMotors). I think you want/need to retrieve the instance of CharacterMotor that is attached to this gameObject.
#pragma strict
function Start () {
var charMotor : CharacterMotor = gameObject.GetComponent(CharacterMotor);
transform.position.x = charMotor.playerx;
}
An instance of type "CharacterMotor" is required to access non static member "playerx"
The above error message describes precisely what is happening. You are just trying to access a variable without first creating an instance of it. Keep in mind that UnityScript != JavaScript.
To fix this issue, simply change
public var playerx : float = transform.position.x;
to
public static var playerx : float = transform.position.x;
Though this fixes your immediate problem I do not recommend continuing down this path. I suggest that you learn other aspects of the language first (such as classes) so that you can better organize and construct your data.
See: http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)
CharacterMotor is the type, there can be multiple instantiations of your type in memory at the same time so when you call the type name you are not referencing any instance in memory.
to get an instance of the type that is connected to you current gameobject try this:
var charactorMotor : CharacterMotor = gameObject.getComponent("CharacterMotor");
Now you have access to that instances properties
transform.position.x = characterMotor.playerx;