Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Antti P · Jun 16, 2010 at 10:20 AM · gameobjectscene

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Antti P · Jun 16, 2010 at 11:13 AM 0
Share

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.

avatar image · Jun 16, 2010 at 11:16 AM 0
Share

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.

avatar image Antti P · Jun 16, 2010 at 11:32 AM 0
Share

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.

avatar image Antti P · Jun 16, 2010 at 11:38 AM 0
Share

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.

avatar image · Jun 16, 2010 at 11:42 AM 0
Share

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.

Show more comments
avatar image
0

Answer by Antti P · Jun 16, 2010 at 11:40 AM

Removed obsolete answer.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How Do You Have Multiple High Scores For 1 GameOverScene? 1 Answer

How to determine bounding box of scene? 2 Answers

Enable/Disable objects, with script 9 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Save game objects to new scene 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges