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 Trorlroprlro · Nov 02, 2013 at 07:52 AM · variable

The more pages the closer he comes.

I'm trying to make for my slender game that every page i collect his teleport radius from player decreases. I know exactly how to make this but how do i access the pageCollect script from my slenderAI script? Here are the both scripts:

slenderAI

 using UnityEngine;
 using System.Collections;
 
 public class slenderAI : MonoBehaviour {
 
     
 public Transform player;
     
     
 public bool isVisible = false;
 
 public AudioClip Static;
     
  
 void Update()
 {    

//just gonna cut everything not wanted

      if(Distance > 80 && timer == 0 && isVisible == false && pages > 1){
                Vector3 position = player.position;
                float distance = Random.Range(20.0f,35.0f);
                Vector3 objPosition = position - player.forward* distance;
                transform.position = objPosition;

}

  if(Distance > 70 && timer == 0 && isVisible == false && pages > 2){
                    Vector3 position = player.position;
                    float distance = Random.Range(18.0f,33.0f);
                    Vector3 objPosition = position - player.forward* distance;
                    transform.position = objPosition;
                transform.position = objPosition;

} //and so on

And here is the pageCollect:

 using UnityEngine;
 using System.Collections;
 
 public class PageCollect : MonoBehaviour {
     
 public int pages = 0;
 
 void Update () {
 
 
         if(pages == 3)Application.LoadLevel(0);
 
         RaycastHit hit;
 
         var fwd = transform.TransformDirection (Vector3.forward);
 
         
 
         if (Input.GetKey(KeyCode.E) || Input.GetMouseButtonDown(0)) {
 
             if (Physics.Raycast(transform.position, fwd, out hit, 2)) {
 
                if (hit.collider.tag == "Finish") {
 
                     Debug.Log ("Page grabbed!");
                     
                     pages +=1;
 
                     Destroy(hit.transform.gameObject);
 
                 }
 
                 else Debug.Log ("Not a page");
 
 
     }
  }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Nov 02, 2013 at 08:00 AM

Did you try reading the manual? Assuming your scripts are attached to the same gameObject:

 OtherScript otherScript = GetComponent(OtherScript);
 otherScript.DoSomething(); 

If they're on different game objects, try reading the following topic: http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

(p.s. this is probably one of the most commonly asked questions on Unity... did you try searching first?)

Comment
Add comment · Show 4 · 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 Trorlroprlro · Nov 02, 2013 at 08:02 AM 0
Share

I did and tried them on my game w/o no luck.

avatar image Trorlroprlro · Nov 02, 2013 at 08:08 AM 0
Share

I tried

 void Start(){
     GetComponent<PageCollect>().pages = 0;
 }

But it gives me:

 Object reference not set to an instance of an object
 slenderAI.Start () (at Assets/Scripts/slenderAI.cs:27)
 
avatar image tanoshimi · Nov 02, 2013 at 09:04 AM 0
Share

Firstly, you should generally cache components returned by GetComponent, espectially if you'll be accessing them frequently:

 PageCollect _pageCollect;
 void Start(){
     _pageCollect = GetComponent<PageCollect>();
     _pageCollect.pages = 0;
 }

Secondly, what actually is line 27 of SlenderAI.cs that is erroring? Your code is cut so I cannot tell.

avatar image Trorlroprlro · Nov 02, 2013 at 09:41 AM 0
Share

Line 27 is: GetComponent().pages = 0 ;

But now it gives me same error on Line 29:

 _pageCollect.pages = 0;

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Another way to reference scripts? 2 Answers

Assigning varaibles to prefabs 1 Answer

Declaring Variable in Child & Access as Parent 2 Answers

Change variable value of a script from another script. both in different Game object 1 Answer


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