x


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");
            }
        }
}
more ▼

asked Jun 16 '10 at 10:20 AM

Antti P gravatar image

Antti P
33 3 3 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jun 16 '10 at 10:31 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

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.

Jun 16 '10 at 11:13 AM Antti P

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.

Jun 16 '10 at 11:16 AM Marowi

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.

Jun 16 '10 at 11:32 AM Antti P

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.

Jun 16 '10 at 11:38 AM Antti P

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.

Jun 16 '10 at 11:42 AM Marowi
(comments are locked)
10|3000 characters needed characters left

Removed obsolete answer.

more ▼

answered Jun 16 '10 at 11:40 AM

Antti P gravatar image

Antti P
33 3 3 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5071
x2084
x716
x2

asked: Jun 16 '10 at 10:20 AM

Seen: 780 times

Last Updated: Jun 16 '10 at 12:12 PM