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 Jack62Lewis · Jul 28, 2013 at 07:03 PM · c#gameobjecttextureinheritance

Lines of code playing multiple times?

Hi,

(Wasn't sure what to name this. ) For a phone game, I'm using Unity's ray casting features to allow me to run scripts when Objects with Colliders are touched. I have this feature working.

But I've ran into a problem when trying to make a script that, if you tap on an object it will cycle through an array of textures. It works sort of but every time I tap the object it runs through 5 textures, not 1 (The number it should be doing per tap). I know it's doing this because I added a debug.log script as well and it tells me that I've apparently tapped the object 5 times. Which is the problem.

I don't think it has anything to do with my TouchLogicFixed script (The script that my TextureChange Script is inheriting from) because I tested the debug.log on other objects and they tell me I've tapped them once. It must be to do with my TextureChange Script below.

 using UnityEngine;
 using System.Collections;
 
 public class NewTex : TouchLogicFixed // The Script that this Script is inheriting form.
 {
 
 
 public Texture[] Level = new Texture[10];
 int Current = 0;
 public GameObject LevelChoose; // I know this isn't necessary since this script is on this object, but it won't be later.
 
 
 
 void Began() //On my TouchLogicFixed Script, when a touch begins on an object it broadcasts this "Began" function for this object to use.
 { 
 
 Current = Current + 1; //This line seems to be executed 5 times.
 Debug.Log("TapBegan"); //This line seems to be executed 5 times.
 }    
 
 void Ended() //On my TouchLogicFixed Script, when a touch ends on an object it broadcasts this "Ended" function for this object to use.
 {
 
 LevelChoose.renderer.material.mainTexture = Level[Current]; //This line seems to be executed 5 times.
 Debug.Log("TapEnd"); //This line seems to be executed 5 times.
 }
 
 
 }


Below is the TouchLogicFixed script where Began() and Ended() are called.

 using UnityEngine;
 using System.Collections;
  
 public class TouchLogicFixed : MonoBehaviour 
 {
  public static int currTouch = 0;//so other scripts can know what touch is currently on screen
  private Ray ray;//this will be the ray that we cast from our touch into the scene
  private RaycastHit rayHitInfo = new RaycastHit();//return the info of the object that was hit by the ray
  
  
  void Update () 
  {
   //is there a touch on screen?
   if(Input.touches.Length <= 0)
   {
    //if no touches put code here if needed
   }
   else //if there is a touch
   {
    //loop through all the the touches on screen
    for(int i = 0; i < Input.touchCount; i++)
    {
     currTouch = i;
                 
     
     //this is for 3d objects with colliders
                 
 
     if(Input.GetTouch(i).phase == TouchPhase.Began)
     {
      ray = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);//creates ray from screen point position
      if(Physics.Raycast(ray, out rayHitInfo))
      {
     
      
       rayHitInfo.transform.gameObject.SendMessage("Began");
                         
                         
                             
                             
      }
     }
     
                 if(Input.GetTouch(i).phase == TouchPhase.Ended)
     {
      ray = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);//creates ray from screen point position
      if(Physics.Raycast(ray, out rayHitInfo))
      {
     
       //Camera.mainCamera.backgroundColor = Color.gray;//test to show it works
       rayHitInfo.transform.gameObject.SendMessage("Ended");
                         
                         
                             
                             
      }
     }
                 
    }
   }
  }
 }
Comment
Add comment · Show 17
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 Graham-Dunnett ♦♦ · Jul 28, 2013 at 07:04 PM 0
Share

Well, since we've no way of knowing how Began() and Ended() are called it's impossible to make any suggestions.

avatar image robertbu · Jul 28, 2013 at 07:36 PM 0
Share

@Graham Dunnett - He's using Send$$anonymous$$essage(): line 36 and 51 of the second script.

avatar image robertbu · Jul 28, 2013 at 07:37 PM 0
Share

@Jack62Lewis - I don't spot anything wrong. Any chance you have (mistakenly) attached this script to more than one object?

avatar image Jamora · Jul 28, 2013 at 07:46 PM 0
Share

How many instances of the TextureChange script do you have attached to rayHitInfo.transform.gameObject?

avatar image Peter G · Jul 28, 2013 at 07:58 PM 1
Share

How many scripts in the scene that inherit from TouchLogic? All the inherited classes will inherit the Uodate() so you could get multiple messages that way if there are multiple children

Show more comments

1 Reply

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

Answer by Peter G · Jul 28, 2013 at 09:07 PM

You just need to add an extra wrapper around the message.

 if(Physics.Raycast(ray, out rayHitInfo))
 {
       if(rayHitInfo.transform == transform)
            SendMessage("Began");
            //or SendMessage("Ended")
            // You need to add the check in both sections.
  }

Just another possible solution. You have basically created a touch handler already. Maybe you remove all the inheritance then just have TouchLogic send messages wherever they need to go. Essentially TouchLogic could be the event handler/distribution class. If you wanted to try that you would ignore my first correction then simply remove the inheritance in all your classes (and add back in the inheritance from MonoBehavior).

Comment
Add comment · Show 1 · 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 Jack62Lewis · Jul 28, 2013 at 10:45 PM 0
Share

Worked. Thanks :)

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

18 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

Related Questions

Load Material to GameObject 2 Answers

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

Select colour then change texture 0 Answers

simple texture question 1 Answer

Efficiency of Game Loops 2 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