- Home /
Tiny tag changing and checking problem
Hi y'all! No I'm not from Texas, fool'd ya right? Ok I'll shush, and get to the question. xD
Anyway here's the script. There's two objects, CargoPad_A and CargoPad_B script. This is CargoPad_A script but its pretty much the same as B. Anyway my problem is with this one.
What its suppose to do is when the player ship lands(collides) on it, check if it collided with a object with a tag "Player", change the player ships tag to "CargoPad_A_Loaded", change the CargoPad's tag to "CargoPad_A_Loaded" and give 500 score to the player(which is in another script), and thus rendering landing on the same pad twice or more, and getting score each time, not possible cause the ship is no longer tagged as "Player". But for some reason I can't figure out, the player gets 500 points if he lands on the same pad again even tho the tag changed, and then passes the level cause you only need 1000 points(load cargo on one pad, unload on another) to finish a level. xD
Already made a few levels with cargo pads, and I didn't notice earlier cause I played it right, and didn't land on the same pad twice. xD
var stationLight : Light;
var onMaterial : Material;
var onColor : Color;
var ship : GameObject;
var shipScript : PlayerController;
var GUI : InGameGUI;
function Start ()
{
shipScript = ship.GetComponent(PlayerController);
GUI = GameObject.FindWithTag("GUI").GetComponent(InGameGUI);
}
function OnCollisionEnter(cargoLoad : Collision)
{
if(cargoLoad.gameObject.tag == "Player")
{
shipScript.LoadCargoA();
ship.gameObject.tag = "CargoPad_A_Loaded";
Activate();
}
else if(cargoLoad.gameObject.tag == "CargoPad_A_Loaded")
{
print("Already loaded...");
}
}
function Activate()
{
audio.Play();
gameObject.tag = "CargoPad_A_Loaded";
renderer.material = onMaterial;
stationLight.color = onColor;
GUI.LZActivated();
}
Can you share the part where you are giving out the points?
Here its in the InGameGUI script. I don't think that's the problem, it just adds points when on collision enter, meaning when I land again, I just want it to change the tag of the player and pad so when I land on the same one again nothing happens. Would it maybe be simpler to disable the pad entirely once I land on it the first time? There isn't use for it after the first time anyway.
function LZActivated()
{
levelScore+=500;
scoreText.text = "score - "+levelScore;
numActivated++;
if(numActivated == totalLZ)
{
Win();
}
}
Disabling is much more simple, but keep in $$anonymous$$d that disabling the object will make it disappear.
Did you check the players tag during run-time to make sure it really changes?
I just did, when it lands, Ship changes its tag to CargoPad_A_Loaded, but the CargoPad never changes, it just remains as CargoPad_A as it is its starting tag.
The if(cargoLoad.gameObject.tag == "Player")
does that check the tag of the ship which is colliding or the tag of the object holding that collider?
Also, ins$$anonymous$$d of disabling, can I just remove the script from it?
It checks the object that collided.
What you are saying is that this line:
gameObject.tag = "CargoPad_A_Loaded";
Is not doing it's job....
Do the rest of the stuff in Activate work?
Yes, you can remove the script using:
Destroy(GetComponent(scriptname));
Answer by ChrisSch · Jul 25, 2013 at 03:02 PM
I fixed it. Rewrote most of the code and made new tags. Changed CargoPadA1 and CargoPadB2 to A1 and A2, cause the first was confusing me a lot. xD Also removed the light and material lines to make it simpler for viewing. I'll post both A1 pad and A2 pad's scripts if anyone needs them or had the same problems with tags. I commented on each line of A1.js to avoid future confusing when I forget how I did all that. xD
A1.js
#pragma strict
//Ship starts off with tag Player
//Cargo pad A1 starts off with tag CargoPadA1
var ship : GameObject; //Ship we'll be accessing the code from to control the Cargo Loaded boolean.
var shipScript : PlayerController; //Ship script we'll be changing the Cargo Loaded boolean.
var GUI : InGameGUI; //GUI script we'll be using to get score from landing on CargoPadA1
function Start ()
{
shipScript = ship.GetComponent(PlayerController); //Get the PlayerController script from the ship
GUI = GameObject.FindWithTag("GUI").GetComponent(InGameGUI); //Get the InGameGUI script from the GUI object
Debug.Log ("Connected to ShipsScript to control cargo load and to GUI for score"); //check if it reached this line
}
function OnCollisionEnter(cargoLoad : Collision)
{
if(cargoLoad.gameObject.tag == "Player") //Our initiate ship tag
{
Debug.Log ("Landed on A1"); //checking if we landed
ship.gameObject.tag = "ShipLoaded"; //changing the ships tag to ShipLoaded so we can no longer land there as Player and get points
Debug.Log ("Changed ships tag to ShipLoaded"); //checking
Activate(); //Getting to function Activate. Purposfuly set in the middle of the code to check if it will work and everything after it
gameObject.tag = "CargoPadALoaded"; //changing A1's tag to CargoPadALoaded, not necessary since we already changed the ships tag, but just in case
Debug.Log ("Changed CargoPads script to CargoPadALoaded"); //checking
shipScript.LoadCargoA(); //Just changing the cargoLoaded boolean to true in the PlayerController script
Debug.Log ("Cargo loaded :)"); //checking if it reached to this point
}
else if(cargoLoad.gameObject.tag != "Player") //if the ship is no longer tagged Player, meaning it has cargo, nothing will happen upon collision
{
Debug.Log ("Already Landed on A1"); //checking if nothing happened
}
}
function Activate()
{
audio.Play(); //playing audio to indicate that the ship has landed
Debug.Log ("Playing audio.");
GUI.LZActivated(); //Giving score to the player in the InGameGUI script
}
And A2.js
#pragma strict
//Cargo pad A2 starts off with tag CargoPadA2
var ship : GameObject;
var shipScript : PlayerController;
var GUI : InGameGUI;
function Start ()
{
shipScript = ship.GetComponent(PlayerController);
GUI = GameObject.FindWithTag("GUI").GetComponent(InGameGUI);
}
function OnCollisionEnter(cargoLoad : Collision)
{
if(cargoLoad.gameObject.tag == "ShipLoaded")
{
Debug.Log ("Landed on A2");
ship.gameObject.tag = "Player";
Debug.Log ("Changed ships tag to Player");
Activate();
gameObject.tag = "CargoPadAUnloaded";
Debug.Log ("Changed CargoPads script to CargoPadAUnloaded");
shipScript.UnloadCargoA();
Debug.Log ("Cargo unloaded :D");
}
else if(cargoLoad.gameObject.tag != "ShipLoaded")
{
Debug.Log ("Go back to A1 and load the cargo first!");
}
}
function Activate()
{
audio.Play();
Debug.Log ("Playing audio.");
GUI.LZActivated();
}
Answer by Seth-Bergman · Jul 25, 2013 at 01:20 PM
judging by your script, you never changed the cargo pad tag, just the ship:
if(cargoLoad.gameObject.tag == "Player")
{
shipScript.LoadCargoA();
ship.gameObject.tag = "CargoPad_A_Loaded"; //change this
cargoLoad.gameObject.tag = "CargoPad_A_Loaded"; // to this
Activate();
}
Didn't I change it with:
gameObject.tag = "CargoPad_A_Loaded";
In Activate()?