I have a node in my scene called hitpoint which I want a MeshInstance to look at
this is my code here:
extends MeshInstance
export(NodePath) var hit_point
func _process(delta):
look_at(hit_point.global_transform.origin, Vector3.UP)
and this is the error that I keep getting, despite having assigned the variable hit_point to the hitpoint node.
invalid get index 'global_transform' (on base: NodePath)
Related
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)
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.
I had this Swift code in Xcode which worked perfectly:
var obstacles = [SKSpriteNode]()
obstacles.append(SKSpriteNode(imageNamed: "Rectangle"))
But I wanted to add an element to this array obstacles in order to have something like obstacles[i].newElement, so I tested this:
class obstacle: SKSpriteNode
{
var isActive = false
}
var obstacles = [obstacle]()
obstacles.append(SKSpriteNode(imageNamed: "Rectangle"))
obstacles[i].isActive = true
But I have an error with the line obstacles.append(SKSpriteNode(imageNamed: "Rectangle"))
which is: "missing argumentent for parameter 'size' in call", the thing is that I hadn't this error before and I don't know what is the problem.
If you could help me, thanks
You have declared the obstacles array as containing instances of obstacle, but you are trying to append an instance of its superclass, SKSpriteNode. Just change that to create an instance of obstacle instead:
obstacles.append(obstacle(imageNamed: "Rectangle"))
Remember that if you have a base class A and a subclass of it B : A, you can pass an instance of B where A is expected, but you cannot pass A where B is expected
Side note: by convention, type names in swift start with an uppercase - I suggest you to keep that convention - so it's better to rename your class as Obstacle
If you created an Array and specified that the type is Obstacle, you must add an Obstacle object.
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.
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;