I'm programming a simple game (the first one I do on my own) in which basically there are two scoring goals, and when a player scores, I want to reset the position for all players to some coordinates.
I have a script attached to the goals, which detects collision with the ball, as follows:
Goal.js
#pragma strict
function OnTriggerEnter2D (hitInfo : Collider2D) {
if (hitInfo.name == "Ball")
{
var wallName = transform.name;
GameManager.Score (wallName);
hitInfo.gameObject.SendMessage ("ResetBall");
//Here I need to call the ResetPlayer function
PlayerControlHS.ResetPlayer();
}
}
The following script is attached to the players
PlayerControlHS.js
#pragma strict
var resetPosX : float;
var resetPosY : float;
//keys
var moveUp : KeyCode;
var moveLeft : KeyCode;
var moveRight : KeyCode;
var speed : int = 4;
function Update () {
if (Input.GetKey(moveUp)) {
rigidbody2D.velocity.y = speed;
}
if (Input.GetKey(moveLeft)) {
rigidbody2D.velocity.x = -speed;
}
else if (Input.GetKey(moveRight)) {
rigidbody2D.velocity.x = speed;
}
else {
rigidbody2D.velocity.x = 0;
}
}
function ResetPlayer () {
Debug.Log("I'm being called");
}
Both the goals and the players have a RigidBody 2D and a collider of some kind, as well as the ball.
I've gotten to get the function called as it is right now, but if I try to modify the position coordinates of a player, I enter a loop of death errors that when I fix one, I get another one.
That happens when I put this code in ResetPlayer():
rigidbody2D.position.x = resetPosX;
rigidbody2D.position.y = resetPosY;
Mostly, the errors are because I need an object of type PlayerControlHS to access those fields. I've tried adding a variable of that type (and referencing it to each player) but it doesn't work because it tells me I need an object to access that variable... I don't know how to initialize it if I make it static.
How could I get it working?
EDIT: I'm thinking the best approach would be sending a message just like the ResetBall one, but it doesn't work (I think because the function is called from ball, object of another type and it can't find the function). This way, the function ResetPlayer could stop being static. But I'm not sure if this is right, since I can't get it without compile errors.
EDIT2: It worked, even though I had to put it one liner
hitInfo.gameObject.GetComponent(PlayerControlHS).ResetPlayer();
Because Unity was telling me that I needed to put a semicolon here (I don't understand :S):
PlayerControlHS playerScript ; = hitInfo.gameObject.getComponent(PlayerControlHS);
But I still can't get to modify the variables of the position...
I have tried fixing it but I get an error saying that "I need an instance of type UnityEngine.Component to access non static member 'rigidBody2D' in a variable declaration I made
static var test : PlayerControlHS = rigidbody2D.GetComponent(PlayerControlHS);
And the code I'm trying in ResetPlayer:
test.GetComponent(PlayerControlHS).rigidbody2D.position.x = test.GetComponent(PlayerControlHS).resetPosX;
test.GetComponent(PlayerControlHS).rigidbody2D.position.y = test.GetComponent(PlayerControlHS).resetPosY;
Try changing this line:
PlayerControlHS.ResetPlayer();
To this:
PlayerControlHS playerScript = hitInfo.gameObject.getComponent(PlayerControlHS);
playerScript.ResetPlayer();
That is how you call a function from another script.
EDIT 1:
Sorry I got mixed up with C# even though you are using javascript, I figured out why you got that error. There's a slight difference in variable declaration.
Change the above code to this:
var playerScript:PlayerControlHS = hitInfo.gameObject.GetComponent(PlayerControlHS);
playerScript.ResetPlayer();
As for resetting the player's position, the script is inside the player game object is it not? Then you can just simplify it like this:
function ResetPlayer () {
rigidbody2D.position.x = resetPosX;
rigidbody2D.position.y = resetPosY;
}
Really sorry for that :(
Related
I'm trying to create an outline when you are near it, but i'm getting all the time the same error.
void Update () {
if (Input.GetKeyDown(KeyCode.E)){
var outline = gameObject.AddComponent<Outline>();
outline.OutlineMode = Outline.Mode.OutlineAll;
outline.OutlineColor = Color.yellow;
outline.OutlineWidth = 5f;
}
}
void OnTriggerStay(Collider other) {
if (Outline.OutlineMode == Outline.Mode.OutlineAll) {
Debug.Log("test");
}
}
If i press E it works, and if i change it to ontriggerstay works too, but im trying that it only applies one time, because im getting some errors if its on. I have to say that im using an asset, called quick outline
Srry for my very bad english and explanation and thank you
add the outline to your object in Awake() then set it to disabled.
then enable it in OnTriggerEnter() and disable it in OnTriggerExit()
this will keep you from making multiple copies, and it will only be active when you are in range of your trigger
I have spent all day looking for a solution to this problem, and I simply can't find one. Using JavaScript in Unity 3D, I have a script where I want to play a sound when the player's velocity on the X axis reaches a certain point, and if it's not at that point, then the sound will be muted. And I believe I have all the structure right, it's just the line of code that says to mute the audio that won't work. I've tried all kinds of different combinations, and I get an error for each one.
The script looks like this:
#pragma strict
var playing = false;
var audioSource = GetComponent.<AudioSource>();
function Update () {
if (transform.GetComponent.<Rigidbody>().velocity.x <= 2.5 &&
transform.GetComponent.<Rigidbody>().velocity.x >= -2.5)
{
Mute();
} else {
Unmute();
}
}
function Mute () {
audioSource.mute = true;
}
function Unmute () {
audioSource.mute = false;
Sound();
}
function Sound () {
if (transform.GetComponent.<Rigidbody>().velocity.x >= 2.5 && playing ==
false)
{
playing = true;
GetComponent.<AudioSource>().Play();
yield WaitForSeconds(2);
playing = false;
}
if (transform.GetComponent.<Rigidbody>().velocity.x <= -2.5 &&
playing == false)
{
playing = true;
GetComponent.<AudioSource>().Play();
yield WaitForSeconds(2);
playing = false;
}
}
I've gotten all kinds of different errors, but the one I seem to be getting the most says "UnityException: GetComponentFastPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'motioncheck' on game object 'Ball'." I'm not sure what this means, since I'm still kinda a nub at JavaScript.
I feel like it shouldn't be this hard to just mute a sound. I'm going to assume that the answer to this is really simple and that I'm just really dumb. That's what usually seems to happen, lol.
In the mean time, I'm going to continue my rampage across the internet in search for answers to this problem.
Your mute code is fine.
"UnityException: GetComponentFastPath is not allowed to be called from
a MonoBehaviour constructor (or instance field initializer), call it
in Awake or Start instead. Called from MonoBehaviour 'motioncheck' on
game object 'Ball'." I'm not sure what this means, since I'm still
kinda a nub at JavaScript.
See this:
var audioSource = GetComponent.<AudioSource>();
That's a Unity API and you have to call their functions from inside a function. The Awake or Start function is appropriate for initializing component variables.
var audioSource : AudioSource;
function Start()
{
audioSource = GetComponent.<AudioSource>();
}
Note that Unityscript/Javascript is now discontinued. They no longer update the doc on this and you cannot create new scripts from the Editor anymore. It still works as for now but the compiler will be removed soon. Please learn and start using C# before its support is totally removed.
Trying to write a script that's on several different objects where on mouseclick, 1 is added to a defined variable which then prints out. Currently when I click the objects, 1 is always printed.
#pragma strict
import UnityEngine.UI;
var theImage:UnityEngine.UI.Image;
var theSprite:Sprite; //The image you want to drag in the inspector
private var clickCount = 0;
function OnMouseDown () {
theImage.sprite = theSprite;
clickCount++;
Debug.Log(clickCount);
}
Here's my script. It also changes a GUI sprite. Seems like an easy fix, not sure where I've gone wrong.
I'm a newby in Unity and I'm following the first Unity tutorial. When i try to run my first script i get this error:
NullReferenceException: Object reference not set to an instance of an object
Here is my script:
#pragma strict
private var rb:Rigidbody;
private var player:GameObject;
function start() {
player = GameObject.Find("Player");
rb = player.GetComponent(Rigidbody);
}
function FixedUpdate() {
var moveHorizontal:float = Input.GetAxis("Horizontal");
var moveVertical:float = Input.GetAxis("Vertical");
var movement:Vector3 = new Vector3(moveHorizontal , 0.0f , moveVertical);
rb.AddForce(movement);
}
I have no idea what am I doing wrong.
UPDATE:
Here is my scene:
UPDATE:
I've put print in both functions, and it seems like start is not being called at all, and that is why my variable is not being initialized. Any idea?
I would remove the declaration
private var rb:Rigidbody;
because it seems that your script is trying to access the declared Rigidbody (that stills not initialized, so it's null), and not the object's real one.
Side note: seems that, from Unity 5.3.3, you have to do:
player.GetComponent.<Rigidbody>();
(from here)
It seems your gameobject doesn't have Rigidbody component attached to it and variable rb is null after rb = GetComponent(Rigidbody);
You should take advantage of "Unity way" to reference variables. I mean, your player and rb attributes must be public and you just drag into it your gameobject from hierarchy to your attribute on inspector.
If you still want to do it private, for some good reason, just change player = GameObject.Find("Player"); for player = GameObject.FindWithTag("Player"); and your null reference probably will be solved.
So finally after a few hours, I got it. The problem was that start function should be upper case Start. Since it was lowercase, it wasn't called and rb was not initialized.
And here is the final script:
#pragma strict
private var rb:Rigidbody;
function Start() {
rb = GetComponent(Rigidbody);
}
function FixedUpdate() {
var moveHorizontal:float = Input.GetAxis("Horizontal");
var moveVertical:float = Input.GetAxis("Vertical");
var movement:Vector3 = new Vector3(moveHorizontal , 0.0f , moveVertical);
rb.AddForce(movement);
}
I'm learning unity scripting. But I don't understand the following :
static private var lastRecalculation = -1;
static function RecalculateValue () {
if (lastRecalculation == Time.frameCount)
return;
ProcessData.AndDoSomeCalculations();
}
I don't get the third line or the condition in the IF statement. I know its a bit amateur, but pls help.
Thank You.
This is from the Time.frameCount documentation. This example shows how to create a function that executes only once per frame, regardless of how many objects you attach your script to. I suspect though that this example is incomplete because it never updates lastRecalculation (or it was assumed you would do so in AndDoSomeCalculations() ).
The reason setting lastRecalculation = -1 initially is so this function runs during the first frame.
Working version:
static var lastRecalculation = -1;
static function RecalculateValue () {
if (lastRecalculation == Time.frameCount) {
return;
}
lastRecalculation = Time.frameCount;
Debug.Log (Time.frameCount);
//ProcessData.AndDoSomeCalculations();
}
function Update () {
RecalculateValue();
}
Attach this script to 2 different GameObjects and run it. You will only see unique frame values 1,2,3,4,5.... even though 2 GameObjects are each calling RecalculateValue()
Now comment out the return and run it again. Now the ProcessData portion of that code runs for both objects every frame and you'll see something like 1,1,2,2,3,3,4,4......