- Home /
Trouble while trying to manipulate object.
Hi there!
I am very new to Unity and scripting, and I havent been able to figure this problem out on my own.
I have a player character, that is created in its own scene, and then preserved when changing scenes. When i decide to apply this piece of script into triggering object(Object that is supposed to move player a bit)
private var playerObject : GameObject;
function OnTriggerEnter(collisionInfo : Collider)
{
if(collisionInfo.gameObject.tag == "Player") {
playerObject.transform.position = Vector3 (1,-8,6);
}
}
I cannot select the playerobject to the script because it doesnt exist in that scene yet.
Now how should/could I properly circumvent this problem, or am I just being a bit silly again?
-EDIT-
I may ha been a bit unclear. I have this object called TriggerButton, which contains the script. Triggerbutton is sitting nicely in its own scene, and waiting for player to come and collide with it. if(collisionInfo.gameObject.tag == "Player") <-This line is already in use and working. What is not working, is that playerObject (tag: Player) does not change its position at collision with TriggerButton, even though other events (playing sound, changing variables) which I left out of this code work perfectly.
-EDIT 2-
Here is the full script: Since I cannot paste long scripts into comments, I made this new "answer" so I could paste the entire script I am using. I did also make some changes to it, but they dont seem to help.
var triggerusable = false; var playerObject : GameObject;
function OnTriggerEnter(collisionInfo : Collider) { print("Collision"); if(collisionInfo.gameObject.tag == "Player") { triggerusable=true; print("You Can Use this thing!"); } }
function OnTriggerExit(collisionInfo : Collider) { print("Collision NOT"); if(collisionInfo.gameObject.tag == "Player") { TextfieldTest.texttestvisible = false; TextfieldTest.texttestphase = 0; triggerusable=false; print("NotColliding!"); } }
function FixedUpdate() { if(Input.GetButton("e")) //- { if(triggerusable) { print("USED!"); triggerusable = false; TextfieldTest.texttestvisible = true; TextfieldTest.texttestphase = 1; playerObject.transform.position = Vector3 (-63.7896,-0.2890768,-3.386028); //This line seems to do nothing, but if I chancge targeted gameobject while running the game it works perfectly. } else { print("Not Usable Anymore"); } } }
Answer by · Jun 16, 2010 at 10:31 AM
As an aside, the Unity Documentation has a Component.CompareTag method.
I'm not sure why, but it seems that the preferred approach would be:
if ( collisionInfo.gameObject.CompareTag("Player") )
The problem with your script is that the playerObject is never defined, which (I now see) is what you were asking.
So, add:
function Start ()
{
playerObject = GameObject.FindWithTag("Player");
}
When this object is created, it will find the playerObject (by tag) and set the variable in this script to point to it, and then changing its transform should be reflected on the real playerObject.
Unfortunately this didnt seem to help. While everything seemed to work nicely and without errors, the object did not react in any way to transform.position command.
I am wondering if I am doing something wrong.
You're actually checking if the thing that the playerObject has bumped into is the "Player". Remove the ifcheck and see if it works. Also of note - the playerObject will always be reset to the same position, but that might be intentional.
Uh, I may ha been a bit unclear.
I have this object called TriggerButton, which contains the script. Triggerbutton is sitting nicely in its own scene, and waiting for player to come and collide with it.
if(collisionInfo.gameObject.tag == "Player") <-This line is already in use and working. What is not working, is that playerObject (tag: Player) does not change its position at collision with TriggerButton, even though other events (playing sound, changing variables) which I left out of this code work perfectly.
I actually have quite a bit more of code in the script that is attached to TriggerButton. I create a new answer and paste the whole script into it.
Oooh, sorry. I thought this script was being attached to the playerObject. You can find the current playerObject when you try to change it's position. I'll update my answer accordingly.