• 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 Moomius · Apr 08, 2014 at 11:49 AM · casting

Accessing a function from another script

Hi. I've struggled to find the answers to these two problems. Primarily, I can't seem to access a function in another script on another game object. It gives me this error: playerMove.Dead() is inaccessible due to its protection level.

Also, in the same code, enemyAI, I can't seem to delete an object after instantiation. It gives me this error: Cannot cast from source type to destination type.

CODE: Player script (The script with the function I am trying to access)

 public class playerMove : MonoBehaviour
 {
     public float speed;
     public float tilt;
     public ParticleSystem deathParticle;
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         
         Vector3 movement = new Vector3 (moveHorizontal, moveVertical, 0.0f);
         rigidbody.velocity = movement * speed;
 
     }
     void Dead(){
         Application.LoadLevel (Application.loadedLevel);
     }
 }

 

Script with the second error + code trying to access player move's function

 public class enemyAI : MonoBehaviour {
 
     public bool smartAI = false;
     public int force = 5;
     public float delay = 5;
 
     public playerMove script;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         rigidbody.AddForce(0.0f ,0.0f, force * -1);
     }
     void OnTriggerEnter(Collider collider){
         if(collider.tag == "Player"){
             Debug.Log ("We've been hit!");
             script.Dead();
         }
     }
 }

using UnityEngine; using System.Collections;

public class spawnPoint : MonoBehaviour {

 private int randomInt;
 public float spawnRate;

 public Rigidbody enemyPrefab;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     randomInt = Random.Range (0,950000);
     if(randomInt < 1000){
     GameObject enemy = (GameObject)Instantiate(enemyPrefab, transform.position, transform.rotation);
     Destroy (enemy, 5);
     }
 }

} MOAR CODE using UnityEngine; using System.Collections;

 public class spawnPoint : MonoBehaviour {
 
     private int randomInt;
     public float spawnRate;
 
     public Rigidbody enemyPrefab;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         randomInt = Random.Range (0,950000);
         if(randomInt < 1000){
         GameObject enemy = (GameObject)Instantiate(enemyPrefab, transform.position, transform.rotation);
         Destroy (enemy, 5);
         }
     }
 }
 
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
Best Answer

Answer by spraycanmansam · Apr 08, 2014 at 11:58 AM

All variables and methods are private by default in C# (in UnityScript they default to public). So change a void method to public void if you need to access an instances method externally. As for your other error, post some code and we'll see what we can do :)

Comment
Add comment · Show 5 · 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 Moomius · Apr 09, 2014 at 10:09 AM 0
Share

Thanks for your response; now just the issue with the code I've edited in

avatar image GrahamReeves ♦♦ · Apr 09, 2014 at 10:45 AM 0
Share

What is the issue with the edited code? $$anonymous$$aybe starting another question might make it more clear?

avatar image Moomius · Apr 10, 2014 at 08:13 AM 0
Share

Under the label "$$anonymous$$OAR CODE"

avatar image spraycanmansam · Apr 10, 2014 at 10:50 AM 0
Share

First guess would be that your instantiating a new rigidbody (enemyPrefab) and trying to cast it to a GameObject.

avatar image Moomius · Apr 10, 2014 at 10:59 AM 0
Share

Yeah, I figured that out after lots of fiddling. Thanks for your help

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

UnityEngine.Input.GetMouseButton(1)) issue 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

Mathf.SmoothStep not quite working. 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